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

How to know the dir :Vector3.MoveTowards

Discussion in 'Scripting' started by shuskry, Mar 23, 2018.

  1. shuskry

    shuskry

    Joined:
    Oct 10, 2015
    Posts:
    462
    Hi everybody !

    i'm doing a 2D game and i want to use Blend tree to set one of my 4 animations animation with the direction of the Player

    I would like to know if there is a possibilty to know what direction a object is going when i use "Vector3.MoveTowards"

    Have a good day :D
     
  2. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,514
    Define direction?

    So with Vector3.MoveTowards you have 2 vector parameters, current and target:
    https://docs.unity3d.com/ScriptReference/Vector3.MoveTowards.html

    If you're using these as positions. Then a vector from current to target is:
    Code (csharp):
    1. Vector3 v = target - current;
    This vector is the direction and magnitude in 3-space. If you pass in 2d vectors you can just ignore the z value of the vectors. OR you can just use the Vector2 version of the method:
    https://docs.unity3d.com/ScriptReference/Vector2.MoveTowards.html

    Either way the arithmetic is basically the same:
    Code (csharp):
    1. Vector2 v = target - current;
    So as stated, that vector has the direction and magnitude in vector form. You can normalize the vector to reduce the magnitude to 1, and only have the direction.

    But I'm willing to bet a Vector with x,y components defining a direction isn't what you're looking to if you're trying to select a single animation of 4.

    So what is 'direction'?

    Is it the angle, as in if the vector was the minutes hand on a clock, the position on the clock?

    Well you can take the atan2 of it to get that:
    Code (csharp):
    1. float a = Mathf.Atan2(v.y, v.x);
    https://docs.unity3d.com/ScriptReference/Mathf.Atan2.html

    This will give you the radians of the +x axis in 2d. So 0 means it's facing right, pi is left, pi/2 is down, and 3pi/4 is up.

    You can of course multiply by Mathf.rad2deg to get it in degrees:
    Code (csharp):
    1. float a = Mathf.Atan2(v.y, v.x) * Mathf.rad2deg;
    And then it'd be 0, 180, 90, and 270 respectively.

    But thing is you probably will get values in between if they're facing diagonally or something. So you can round to the nearest 90 to get the best choice, and even get it as an index of 0->3 for each anim:

    Code (csharp):
    1. int index = (int)((Mathf.Round(a / 90f) + 4) % 4); //add a modulo over 4 to get a normalized index
     
    TheUnknown7 likes this.
  3. shuskry

    shuskry

    Joined:
    Oct 10, 2015
    Posts:
    462
    Oh ! That is the greatest answer that i can hope ! Thanks a lot this is perfect!

    I take advantage of your knowledge to ask a last thing, there is a way to adapt a speed( with Transform.Movetoward) with a velocity ?

    in my game i have some object wich use rb.velocity, and other use Vector3.towards

    but i can't find a coefficient with these two parameter :/

    Tanks again for your help i really appreciate!
     
  4. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Just out of curiosity, was that for a 2d blend tree type thing?

    As for your other question, you can think of velocity as meters/second. The magnitude of the velocity could be used as the 3rd parameter for MoveTowards (well, then multiplied by Time.deltaTime*).
    I'm pretty sure going in the other direction, you would normalize your vector and apply the value of your speed to it.
     
  5. shuskry

    shuskry

    Joined:
    Oct 10, 2015
    Posts:
    462
    thanks for your anwser @methos5k

    i think about it too later ^^' i have no sleep since 24hours so ...

    Finally i used Vector3.normelized , i sucess to set float to 2D Directionnal Blend tree and it's work perfectly ! :D

    Thanks agan for your help ! And see you in a few day x)