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
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Rotate object based on movement of another object

Discussion in 'Scripting' started by SpiderJones, Nov 17, 2018.

  1. SpiderJones

    SpiderJones

    Joined:
    Mar 29, 2014
    Posts:
    224
    Hi,

    I'm trying to rotate one object(A) based on the direction of another object(B). I don't want to use something like LookAT, I don't want object A to look at object B.

    This is for using the Vive controller and "spinning" or rotating an object.

    I'm thinking I could get the cosine from the start position of object B, then a later position of object B, (Object B is moving) and then get the cosine between Object A and Object B. And I would somehow apply that angle differences to Object A so it would rotate with Object B as it moved.

    Any tips?

    Thanks!
     
  2. PrieDayThor

    PrieDayThor

    Joined:
    Jun 18, 2015
    Posts:
    16
    Hey

    I hope I understood correctly. So you could use an event and delegate for the objects. So if objectB moves if fires an event and ObjectA reacts to this Event. Code would look something like this:

    Code (CSharp):
    1. public class ObjectB : MonoBehaviour
    2. {
    3.     #region Fields
    4.  
    5.     public float Angle = 10.0f;
    6.  
    7.     #endregion
    8.  
    9.     #region Events
    10.  
    11.     public delegate void ObjectRotationEvent(float pAngle);
    12.  
    13.     public event ObjectRotationEvent EventObjectRotated;
    14.  
    15.     #endregion
    16.  
    17.     #region Methods
    18.  
    19.     private void Update()
    20.     {
    21.         float deltaAngle = Angle * Time.deltaTime;
    22.  
    23.         if (Input.GetKey(KeyCode.D))
    24.         {
    25.             transform.Rotate(new Vector3(1.0f, 0.0f, 0.0f), deltaAngle, Space.Self);
    26.             //OLD SYMTAX FOR PRE C# 4.0
    27.             if(EventObjectRotated != null)
    28.             {
    29.                 EventObjectRotated(deltaAngle);
    30.             }
    31.             // EventObjectRotated?.Invoke(deltaAngle); NEW SYNTAX FOR C# 4.0
    32.         }
    33.     }  
    34.     #endregion
    35. }
    36.  
    37. public class ObjectA : MonoBehaviour
    38. {
    39.     #region Methods
    40.  
    41.     private void Awake()
    42.     {
    43.         FindObjectOfType<ObjectB>().EventObjectRotated += OnEventObjectBRotated;
    44.     }
    45.  
    46.     private void OnEventObjectBRotated(float pAngle)
    47.     {
    48.         transform.Rotate(new Vector3(1.0f, 0.0f, 0.0f), pAngle, Space.Self);
    49.     }
    50.  
    51.     #endregion
    52. }
    Important to note!
    This code would lead to objectA copying the rotation of objectB. A simpler but less performant approach would be the following:

    Code (CSharp):
    1. public class ObjectA : MonoBehaviour
    2. {
    3.     #region Fields
    4.  
    5.     public ObjectB B;
    6.  
    7.     #endregion
    8.  
    9.     #region Methods
    10.  
    11.     private void Awake()
    12.     {
    13.         B = FindObjectOfType<ObjectB>();
    14.     }
    15.  
    16.     private void Update()
    17.     {
    18.         transform.rotation = B.transform.rotation;
    19.     }
    20.  
    21.     #endregion
    22. }
     
  3. SpiderJones

    SpiderJones

    Joined:
    Mar 29, 2014
    Posts:
    224
    Hi, thank you for help. That's very interesting, but it's not exactly what I'm trying to do. I need object A to rotate based on object B's movement not its rotation.

    Please look at the image I've included.

    As B moves through A I need A to rotate in the direction of B's movement.

    The application is for the Vive controller and allows the user to rotate an object by "pushing" it.

    A rotates on any axis, and rotates in the direction of B's moving direction. So if B is moving along the Z and Y access to the right of A, then A would rotate counterclockwise (topdown) on its X and Y axis. If B is moving along on only the Z axis and to the right of A, then A would rotate counter clockwise (topdown) only on its Y axis. A behaves like a ball that has a fixed position but can rotate when someone slides their hand over its surface.

    Thanks!

    Screen Shot 2018-11-17 at 1.16.24 PM.png
     
    Last edited: Nov 18, 2018
  4. SpiderJones

    SpiderJones

    Joined:
    Mar 29, 2014
    Posts:
    224
    Using Vive's input system, I know when B enters and leaves A. And a script attached to A can access B.