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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

[Solved] Rotation of an arrow that shows the direction of the force

Discussion in 'Scripting' started by Davidowa, May 28, 2018.

  1. Davidowa

    Davidowa

    Joined:
    Aug 11, 2017
    Posts:
    6
    Hello everyone,

    I'm having some troubles understanding rotations in Unity v2018.

    I'm trying to locate an arrow that points out the direction of the force that an sphere feels according to the origin (0,0,0).

    The way I'm calculating the force vector is as follows

    Code (CSharp):
    1. float distance = position.magnitude;
    2. Vector3 force = (distance > 0.5) ? (q1 * q2 / (distance * distance)) * position.normalized : Vector3.zero;
    Q1 and Q2 are the values of the spheres and position is the position of Q2 (the one that I can control).

    The way I'm relocating the arrow is as follows:

    Code (CSharp):
    1.            float angleArrow = Mathf.Acos(Vector3.Dot(new Vector3(0, 1, 0), force) / force.magnitude);
    2. Vector3 axisArrowRot = Vector3.Cross(new Vector3(0, 1, 0), force);
    3. arrow.transform.rotation = Quaternion.identity * Quaternion.LookRotation(axisArrowRot * angleArrow);
    Currently what I get is the following behaviour:



    The thing is that i want the arrow follow the direction of the orange line.

    I hope you could help me understand what my problem is.
     
  2. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,851
    There are some strange things in your code for sure, like multiplying by Quaternion.identity (which does nothing), or multiplying axisArrowRot by a float before passing it to LookRotation (which would also do nothing — what matters is the direction, not the magnitude).

    I think all you should have to do for this is something like

    Code (csharp):
    1. arrow.transform.rotation = Quaternion.LookRotation(Q2pos - Q1pos);
    where Q1pos and Q2pos are the positions of the two spheres.
     
  3. Davidowa

    Davidowa

    Joined:
    Aug 11, 2017
    Posts:
    6
    It still does not behavior as expected :(. I erased the Quaternion.identity, thanks for pointing it out :)
     
  4. rakkarage

    rakkarage

    Joined:
    Feb 3, 2014
    Posts:
    683
    The answer is always localRotation instead of rotation
    *edit: usually*
     
    Last edited: May 29, 2018
  5. Davidowa

    Davidowa

    Joined:
    Aug 11, 2017
    Posts:
    6
    I tried to change it into
    Code (CSharp):
    1. arrow.transform.localRotation= Quaternion.LookRotation(axisArrowRot * angleArrow);
    but same behavior :(
     
  6. Davidowa

    Davidowa

    Joined:
    Aug 11, 2017
    Posts:
    6
    I created a base case with the behavior explained at the beginning of the thread. I hope it helps the community and you guys to understand my problem :)
     

    Attached Files:

  7. Davidowa

    Davidowa

    Joined:
    Aug 11, 2017
    Posts:
    6
    Solved, I just changed the following lines in the code of AvatarScript uploaded
    Code (CSharp):
    1. arrow.transform.position = transform.position;
    2.            
    3.             float angleArrow = Mathf.Acos(Vector3.Dot(new Vector3(0, 1, 0), force) / force.magnitude);
    4.             Vector3 axisArrowRot = Vector3.Cross(new Vector3(0, 1, 0), force);
    5.             arrow.transform.rotation = Quaternion.identity * Quaternion.LookRotation(force, axisArrowRot * angleArrow);
    And got the desired behavior