Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

[c#] Using a vector3 in a switch statement

Discussion in 'Scripting' started by kevdotbadger, Mar 3, 2013.

  1. kevdotbadger

    kevdotbadger

    Joined:
    Sep 13, 2011
    Posts:
    82
    Hello, i'm looking for a way to determine which way my character is pointing (north, north_east, east, etc).

    I have a load of if statements to do this at the moment, but it's messy. It also seems that i cannot use a Vector3 in the switch statement.

    Any idea's how I would do this?

    Here's how it looks presently

     
    Last edited: Mar 3, 2013
  2. vagos21

    vagos21

    Joined:
    Sep 3, 2012
    Posts:
    18
    How about reading the Y rotation of your object and then do an integer division of the angle by let's say...45 (each direction changes per 45 degrees) and then use that result in a case to determine which direction it's facing?

    so for example let's say your gameobject's (world) Y rotation is at 240degrees, 240/45=5

    and the cases would be like
    0 north
    1 northeast
    2 east
    3 southeast
    4 south
    5 southwest
    6 west
    7 northwest

    just giving hints :)
     
    Last edited: Mar 4, 2013
  3. hpjohn

    hpjohn

    Joined:
    Aug 14, 2012
    Posts:
    2,190
    switch works fine with a V3, but be aware, when you check for V3 == V3 it checks for EXACTLY the same,
    so V3(1, 0.0001, 0) is never the same as V3(1, 0, 0)
    and doing checks with values calculated from velocity and so on will nearly always have floating decimal values
     
  4. scarpelius

    scarpelius

    Joined:
    Aug 19, 2007
    Posts:
    966
  5. kevdotbadger

    kevdotbadger

    Joined:
    Sep 13, 2011
    Posts:
    82
    Hello, thanks for the input. My game is a 2d top down game, so I was managing the direction they are looking at to determine what animation i should be playing.

    So I don't think using Vector3.Angle() would work.
     
  6. Annihlator

    Annihlator

    Joined:
    Oct 15, 2012
    Posts:
    378
    Ofcourse Vector3.Angle() still works, your game may be displayed in 2d, but unity's worldspace is also fully 3d. your third dimension is just "flat".
    However for calculating angles for Vector2's, i found this topic in Unity Answers you may be interested in, too. (even though this seems to be a C# implementation instead of Java.) http://answers.unity3d.com/questions/162177/vector2angles-direction.html
    Scarpelius' method should work fine too though!
     
  7. kevdotbadger

    kevdotbadger

    Joined:
    Sep 13, 2011
    Posts:
    82
    Thanks guys, both answer contained excellent advice and using the link from Annihlator's post I was able to set this up like I wanted. Also learnt about the Vector3.Cross() method.
     
  8. barisaxo

    barisaxo

    Joined:
    Nov 21, 2019
    Posts:
    3
    I didn't test the Vec3ToDir function... but this is an example of how you must use the when keyword to implement the switch case to compare Vector3s.

    Code (CSharp):
    1.  
    2.  
    3.     Direction GetFacing(Vector3 facing)
    4.     {
    5.         return Ve3ToDir(facing) switch
    6.         {
    7.             _ when Ve3ToDir(facing).Equals(new Vector2(1, -1)) => Direction.SE,
    8.             _ when Ve3ToDir(facing).Equals(new Vector2(0, -1)) => Direction.S,
    9.             _ when Ve3ToDir(facing).Equals(new Vector2(1, 0)) => Direction.E,
    10.             _ when Ve3ToDir(facing).Equals(new Vector2(-1, -1)) => Direction.SW,
    11.             _ when Ve3ToDir(facing).Equals(new Vector2(-1, 0)) => Direction.W,
    12.             _ when Ve3ToDir(facing).Equals(new Vector2(-1, 1)) => Direction.NW,
    13.             _ when Ve3ToDir(facing).Equals(new Vector2(0, 1)) => Direction.N,
    14.         };
    15.  
    16.         Vector2 Vec3ToDir(Vector3 facing)
    17.         {
    18.             int x = 0; int y = 0;
    19.             float dirTolerance = 45;//whatever works best for you
    20.  
    21.             if (facing.x <= -dirTolerance) { x = -1; }
    22.             else if (facing.x >= dirTolerance) { x = 1; }
    23.  
    24.             if (facing.y <= -dirTolerance) { y = -1; }
    25.             else if (facing.y >= dirTolerance) { y = 1; }
    26.  
    27.             return new Vector2(x, y);
    28.         }
    29.  
    30.     }
    31.     enum Direction { N, E, S, W, NE, NW, SE, SW }
    32.  
    33.  
    34.  
    Another example that I have used in my own projects:

    Code (CSharp):
    1.  
    2.  
    3. int Rotation()
    4.     {
    5.  int degrees = (int)(57.3 * Mathf.Atan2(Mathf.Abs(z), Mathf.Abs(x)));
    6.  
    7.               return Quadrant(x, z) switch
    8.                 {
    9.                     var NE when NE.Equals(Quadrant(1, 1)) => 270 - degrees,
    10.  
    11.                     var SE when SE.Equals(Quadrant(1, -1)) => 270 + degrees,
    12.  
    13.                     var NW when NW.Equals(Quadrant(-1, 1)) => 90 + degrees,
    14.  
    15.                     var SW when SW.Equals(Quadrant(-1, -1)) => 90 - degrees
    16.                 };
    17.             }
    18.            
    19.         }
    20.  
    21.         Vector2 Quadrant(float x, float z)
    22.         { return new Vector2((int)Mathf.Sign(x), (int)Mathf.Sign(z)); }
    23. }
    24.  
     
  9. orionsyndrome

    orionsyndrome

    Joined:
    May 4, 2014
    Posts:
    3,070
    ericbegue likes this.