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 to Bind a gameobject Rotation to another gameobject Position?

Discussion in 'Scripting' started by iftah, Apr 15, 2014.

  1. iftah

    iftah

    Joined:
    Feb 25, 2014
    Posts:
    16
    Hi,

    I want to control the rotation of a gameobject B using the translation of another gameobject A like in the image bellow.
    $BindRotation to translation.jpg
    To be exact:
    - When Object A transform.position.x = +x, Object B transform.rotation.z = +180
    - The same for the other axes.

    Any ideas how to do that?

    Thank you.
     
  2. SunnySunshine

    SunnySunshine

    Joined:
    May 18, 2009
    Posts:
    952
    Last edited: Apr 15, 2014
  3. iftah

    iftah

    Joined:
    Feb 25, 2014
    Posts:
    16
    Thank you for your reply,

    I attached the following script to Object A,

    Code (csharp):
    1. public class move : MonoBehaviour {
    2.  
    3.     public GameObject objectB;
    4.  
    5.     void Update () {
    6.         Vector3 relative = transform.InverseTransformPoint(transform.position);
    7.         float angle = Mathf.Atan2(relative.x, relative.y) * Mathf.Rad2Deg;
    8.         print (angle);
    9.         objectB.transform.Rotate(0, angle, 0);
    10.     }
    11. }
    But the value of "angle" is always 0, I think I missed something in the solution you proposed.
    Any ideas what's wrong with my script??!!
     
  4. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    Code (csharp):
    1.  
    2. Vector3 relative = transform.InverseTransformPoint(transform.position);
    3.  
    should be

    Code (csharp):
    1.  
    2. Vector3 relative = transform.InverseTransformPoint(objectB.transform.position);
    3.  
     
  5. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,738
    You're telling Unity, "Okay, take your own world-space position (transform.position), and tell me what that position is in your own local space (transform.InverseTransformPoint)". Note that these two are the same - "relative" is always going to come out to 0,0,0. (Or within a rounding error of that, anyway)

    One of those "transform." in your code should be "objectB.transform.", I think.
     
  6. iftah

    iftah

    Joined:
    Feb 25, 2014
    Posts:
    16
    Thank you all for your help,

    I changed the code as you suggested:

    Code (csharp):
    1. Vector3 relative = transform.InverseTransformPoint(objectB.transform.position);
    2.         float angle = Mathf.Atan2(relative.x, relative.y) * Mathf.Rad2Deg;
    3.         objectB.transform.rotation = Quaternion.Euler(0, 0, angle);
    But, it's not behaving the way I expected, when I move Object A along the X axes with Y=0, Object B rotation around Z axes just snaps between +180° and -180°.
    I want a smooth transition between these two values when I move Object A along the X axes with Y=0.

    Thank you very much for your time.
     
  7. SunnySunshine

    SunnySunshine

    Joined:
    May 18, 2009
    Posts:
    952
    You could do something like this:

    Code (csharp):
    1.  
    2. // Smooth
    3. objectB.transform.rotation = Quaternion.Slerp(objectB.transform.rotation, Quaternion.Euler(0, 0, angle), Time.deltaTime * 2);
     
  8. seejayjames

    seejayjames

    Joined:
    Jan 28, 2013
    Posts:
    685
    You might be hitting a "corner case" with y=0. Do the other values of y behave correctly? If so, you need to test for the y=0 case and create a workaround. This can happen with trig functions...or really, any function with a division, because you might divide by 0 inadvertently.