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

Get rotation from two points in sphere

Discussion in 'Scripting' started by Juli15, Apr 3, 2020.

  1. Juli15

    Juli15

    Joined:
    Jul 7, 2018
    Posts:
    2
    Good morning everyone!

    I've been a few days struggling with this and I've finally decided to post it here as I'm feeling totally lost...

    The situation is, when I press a button, I create a new Gameobject (sphere collider which isTrigger = true) in which my hand is the center

    I created a code with "OnTriggerExit" and when my hand leaves the sphere, I have that position and I instantiate an object in the map

    The problem is that this object is always instantiated based on the rotation of the hand with and offset
    Instantiate(NewObject, myHand.position + (offset), myHand.rotation);


    But what I really want is to get the vector formed by the center of the sphere and the point where my hand left the sphere and make that object to have this rotation.
    Vector3 relPosition = (sphere.position - positionWhenHandLeft.position);

    I think I should use something like this relPosition in some code like this one:
    Instantiate(NewObject, myHand.position + (offset), myHand.rotation);


    Cases:
    If I push the button the sphere appears and If I leave the sphere moving my hand to the right, I want the object to be rotated 90º in the Z axis and appear in horizontal to the right.

    If in other case If I leave the sphere pointing forward diagonal I want the object to appear in the same direction that my hand did leaving the sphere

    I hope some of you find this easy to solve and can help me, If you want I can show you all that I've tried which goes from "LookAt", "RotateTowards" and a lot of weird inventions.. Really thank you!
     

    Attached Files:

  2. orionsyndrome

    orionsyndrome

    Joined:
    May 4, 2014
    Posts:
    3,043
    Based on the title alone, if you need to find a rotation between two points on a sphere (A and B), for a sphere at some center C, make two unit vectors C->A and C->B then use Quaternion.FromToRotation to determine the final quaternion of the rotation.

    Code (csharp):
    1. Vector3 a = /**/ ;
    2. Vector3 b = /**/ ;
    3. Vector3 c = /**/ ;
    4.  
    5. var q = Quaternion.FromToRotation((a - c).normalized, (b - c).normalized);
    Additionally, you can determine the angle between these unit vectors C->A and C->B
    like this
    Code (csharp):
    1. // angle is in radians and always in interval [0 - Pi); i.e. it always gives the smaller angle
    2. static public float CalcCentralAngle(Vector3 dir1, Vector3 dir2)
    3.   => Mathf.Atan2(Vector3.Cross(dir1, dir2).magnitude, Vector3.Dot(dir1, dir2));
    Or alternatively, if you know the shortest distance between A and B (known as chord length),
    you can do
    Code (csharp):
    1. static public float CalcCentralAngle(float chordLength, float radius = 1f)
    2.   => 2f * Mathf.Asin(chordLength / (2f * radius));
    You can also determine the (minor) arc length of that great circle if you
    multiply radius with that central angle
    Code (csharp):
    1. var arcLength = radius * angle; // angle in radians
    to convert angle to degrees
    angle *= Mathf.Rad2Deg;

    and back to radians
    angle *= Mathf.Deg2Rad;


    Right now I'm a bit slow (it's very early in the morning where I am, and I haven't had a night of sleep yet) to understand what you need exactly, so tomorrow!
     
    Last edited: Apr 3, 2020
  3. Juli15

    Juli15

    Joined:
    Jul 7, 2018
    Posts:
    2
    Hi Orionsyndrome!

    I have to say that your answer is beautiful. Yes, beautiful as everything that you said is not exactly what I was expecting to find but it helped me a lot to figure out how to do a lot of things that I've had in my head and yes, finally, to get with the solution that I was looking for!

    Thanks again for your explanation and examples, they helped me a lot! :)
     
    orionsyndrome likes this.
  4. orionsyndrome

    orionsyndrome

    Joined:
    May 4, 2014
    Posts:
    3,043
    hey that's great, here are some links (to my other posts) if you want to learn more about what Quaternion.FromToRotation and Quaternion.Euler do, it might help you discover a dimension of quaternions you maybe didn't know about.
     
    Last edited: Apr 5, 2020