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

how is the direction of a vector calculated?

Discussion in 'Scripting' started by theRelation, Oct 11, 2014.

  1. theRelation

    theRelation

    Joined:
    Aug 18, 2014
    Posts:
    33
    i've been having a hard time coming up with a more stable condition for transitions between the different animations i've developed to express the direction that my character sprites are walking in (i.e. the transform relative to its target over there).

    it's a top down 2.5d game.

    i currently use the movement of the rigidbody and it makes the transitions between the different animations stutter.

    i'm wondering if i could write a script that says when the direction of the movement is such-and-such play this animation? how would i express that? i believe that i should then be able to specify a range (N, NW, W, SW etc.) in degrees between the animations being blended and do away with all the twitchy incremental velocity silliness.

    thanks so much!
     
    Last edited: Oct 11, 2014
  2. Magiichan

    Magiichan

    Joined:
    Jan 5, 2014
    Posts:
    403
    (lastPosition - currentPosition or rigidbody.velocity.normalized) is basically the direction that the player is moving in.
    So you could simply compare the compass direction with the current direction using a built-in unity function like Vector3.Angle. I hope that you can figure out how to implement it by yourself, so you'll actually learn from it.
    But let me know if you really want some example code...
     
  3. theRelation

    theRelation

    Joined:
    Aug 18, 2014
    Posts:
    33
    thanks so much! that sounds exactly like what i'm looking for! so
    Code (CSharp):
    1. Vector3.Angle
    would need to be compared to a "compass direction", expressed how? exactly: numerically? or would it be a struct like a
    Code (CSharp):
    1. Vector3
     
  4. Magiichan

    Magiichan

    Joined:
    Jan 5, 2014
    Posts:
    403
    You basically want to loop through all the the angles, and check to which it's closest to on the compass.
    Vector3.Angle returns the amount of degrees that it variates. The one with the lowest degree variation is the direction that you're moving in.
    here's the code that I wrote for it.
    You might want to change line 25.

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class DirectionScript : MonoBehaviour {
    5.  
    6.     public Compass CurrentDirection;
    7.     private Vector3[] Directions;
    8.  
    9.     public enum Compass { N, NE, E, SE, S, SW, W, NW }
    10.  
    11.     void Start () {
    12.         Directions = new Vector3[8];
    13.         Directions[(int)Compass.N] = Vector3.up;
    14.         Directions[(int)Compass.NE] = (Vector3.up + Vector3.right).normalized;
    15.         Directions[(int)Compass.E] = Vector3.right;
    16.         Directions[(int)Compass.SE] = (Vector3.down + Vector3.right).normalized;
    17.         Directions[(int)Compass.S] = Vector3.down;
    18.         Directions[(int)Compass.SW] = (Vector3.down + Vector3.left).normalized;
    19.         Directions[(int)Compass.W] = Vector3.left;
    20.         Directions[(int)Compass.NW] = (Vector3.up + Vector3.left).normalized;
    21.     }
    22.  
    23.  
    24.     void Update () {
    25.         Vector3 dir = (targetPos - transform.position).normalized;
    26.  
    27.         float closestAngle = 180;
    28.         Compass closestCompass = Compass.N;
    29.         for(int i = 0; i < 8; i++) {
    30.             float _angle = Vector3.Angle (dir, Directions[i]);
    31.             if (_angle < closestAngle) {
    32.                 closestAngle = _angle;
    33.                 closestCompass = (Compass)i;
    34.             }
    35.         }
    36.         CurrentDirection = closestCompass;
    37.     }
    38. }
    39.  
     
    Last edited: Oct 12, 2014
    PunchTheBrain likes this.
  5. theRelation

    theRelation

    Joined:
    Aug 18, 2014
    Posts:
    33
    this is super-exciting! i can't wait to try this! i've been going crazy trying to figure out how to get the characters' transitions to not flicker between! thank you!!!!

    [UPDATE] so, i tried it out and i noticed that with my top-down 2.5d game that moves on x-z it only says "east" and "west," when i move my player character past the follower NPC character with the compass script attached (when you said i should strike line 25 i immediately set about figuring out how to make it work in defiance!). this is what throws me off with the vector3.forward and vector3.backward stuff, because i don't know how it works if you flip the orientation of your game.

    i added a jump feature which does an illusion to make movement on the y visible on z, and when i jump in the air above the follower character NPC sprite, it says "northeast!" i'll have to research how to re-orient the compass to fit the game i'm doing, unless you have any suggestions at the ready...

    [UPDATE II] lol i'm stupid, i get it. thanks!!!
     
    Last edited: Oct 12, 2014
  6. Magiichan

    Magiichan

    Joined:
    Jan 5, 2014
    Posts:
    403
    Did you get it to work? o:
    you'll have to replace Vector3.up with Vector3.forward in the Start function,
    if you want to use x & y. =P
     
  7. theRelation

    theRelation

    Joined:
    Aug 18, 2014
    Posts:
    33
    yeah i figured it out! it works perfect! thanks so much!
     
  8. Magiichan

    Magiichan

    Joined:
    Jan 5, 2014
    Posts:
    403
    Glad I could help.