Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

facing direction check?

Discussion in 'Scripting' started by dr_jelly, Oct 12, 2020.

  1. dr_jelly

    dr_jelly

    Joined:
    Oct 10, 2020
    Posts:
    7
    So in my game I'm trying to change a material on my cube depending on what way an object is facing, I have the material change code done and ready but i have no idea how to detect what way the cube is facing in order to change the material!

    note : I'm a beginner

    useful mentions : I'm making an RPG and my player cube is just a billboard pointing towards the camera and I have 4 materials for player moving up, left, right and down.
    usefull image.png
     
  2. WarmedxMints

    WarmedxMints

    Joined:
    Feb 6, 2017
    Posts:
    1,035
    You could take a direction of the transform, say the forward direction vector, compare the dot product of that directional vector with the desired directions, and return the one it is closest too.

    Although, how is the direction of your character altered? If it is just through user input, you could just set it when they move.
     
  3. TheOtherUserName

    TheOtherUserName

    Joined:
    May 30, 2020
    Posts:
    136
    You could also check the rotation and change it depended on which statement is true:

    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class Exsample : MonoBehavior
    4. {
    5.     public Transform player;
    6.     public Materials[] playerMats;
    7.  
    8.     private void Update()
    9.     {
    10.         switch(player.localRotation)
    11.         {
    12.  
    13.             case 90:
    14.                 player.gameObject.GetComponent<MeshRenderer>().sharedMaterial = playerMats[1];
    15. break;
    16.  
    17.             case 180:
    18.                 player.gameObject.GetComponent<MeshRenderer>().sharedMaterial = playerMats[2];
    19. break;
    20.  
    21.             case -90:
    22.                 player.gameObject.GetComponent<MeshRenderer>().sharedMaterial = playerMats[3];
    23. break;
    24.  
    25.             default:
    26.                 player.gameObject.GetComponent<MeshRenderer>().sharedMaterial = playerMats[0];
    27. break;
    28.         }
    29.     }
    30. }
     
  4. Owen-Reynolds

    Owen-Reynolds

    Joined:
    Feb 15, 2012
    Posts:
    1,920
    TheOtherGameUersName: that's for 2D? If it's 3D there are 3 rotations, so same idea but a little more complicated.

    But as WarMints wrote, you'd do something like that only when the user pressed the arrow to move. Or there would be a function "changeFacingImage()" with that inside, called on keypresses, or bouncing, or a "fear" spell" -- anything that changes direction.
     
  5. TheOtherUserName

    TheOtherUserName

    Joined:
    May 30, 2020
    Posts:
    136
    Owen-Reynolds surely it is a bit more complicated in 3d and I noticed I forgot a .y in my switch/case statement but If you want to have a changeFacingImage() Function you would still need a reference to the current Y-Rotation, and sort through it via if/else or switch, for exsample:
    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class ExsampleTwo : MonoBehavior
    4. {
    5.     public Transform player;
    6.     public Material[] playerMats = new Material[4];
    7.  
    8.     private void Update()
    9.     {
    10.         changeFacingImage(player.localRotation.y);
    11.     }
    12.  
    13.     void changeFacingImage(float currY)
    14.     {
    15.         switch(currY)
    16.         {
    17.             case 0:
    18.                 player.gameObject.GetComponent<MeshRenderer>().sharedMaterial = playerMats[0];
    19.                 break;
    20.  
    21.             case 90:
    22.                 player.gameObject.GetComponent<MeshRenderer>().sharedMaterial = playerMats[1];
    23.                 break;
    24.  
    25.             case 180:
    26.                 player.gameObject.GetComponent<MeshRenderer>().sharedMaterial = playerMats[2];
    27.                 break;
    28.  
    29.             case -90:
    30.                 player.gameObject.GetComponent<MeshRenderer>().sharedMaterial = playerMats[3];
    31.                 break;
    32.  
    33.         }
    34.     }
    35. }
    There is no real diffrence to my first code therefore it doesn't really matter which way you do it.
    Please tell me if I understood you wrong.
     
  6. WarmedxMints

    WarmedxMints

    Joined:
    Feb 6, 2017
    Posts:
    1,035
    Comparing an int with a float is not going to work. A float rarely has a .000 value. Take the forward vector of the object and compare its dot product with the rotations you want to check. Find out which has the highest value and therefore the clostest to your desired angle.
     
  7. Owen-Reynolds

    Owen-Reynolds

    Joined:
    Feb 15, 2012
    Posts:
    1,920
    It de[ends on what dr_jelly's set-up is. If could be Unity's 2D set-up. But it could be moving in 2D, but using regular 3D Unity. If the later, it could be on the moving on "the ground" (x&z) or along x&y (which is how Unity2D does it). That matters since the spin will be different -- either y or z. It even looks a little like a sprite on a canvas, over a 3D area. You wouldn't even use degrees to move for that. If it's manually being set to either 0,90,180 or 270 then comparing to those exact #'s is fine. If it can freely spin then you'd need to check for the nearest of those instead. There's no way to know without details.