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

[Solved] Keep two objects rotating one next to each other

Discussion in 'Scripting' started by ericmartinez, Apr 23, 2015.

  1. ericmartinez

    ericmartinez

    Joined:
    Feb 26, 2014
    Posts:
    9
    Hello!

    My first post in the forum.

    I've been playing with Unity for a few weeks and I'm trying to make an object (a companion you could say) stay always to the left of the main object.

    This is what I have so far

    Code (csharp):
    1.  
    2.         // This rotates the object on Y axis
    3.         if (Input.GetKey (KeyCode.RightArrow)) {
    4.  
    5.             rigidBody.transform.Rotate (0, 6.0f * 10.0f * Time.deltaTime, 0);
    6.             secondObject.transform.Rotate(0, 6.0f * 10.0f * Time.deltaTime, 0);
    7.         }
    8.        
    9.         if(Input.GetKey(KeyCode.UpArrow)) {
    10.            
    11.             rigidBody.position += mainCamera.transform.forward * 1.5f * Time.deltaTime;//Vector3.forward * 1.5f * Time.deltaTime;
    12.         }
    13.        
    14.  
    The code above moves the main object to the right on its own Y axis and the second object on its own Y axis which is not what I want. The second object appears to the left of the main object with something like secondObject.x = mainObject.x-1f (something like that).

    The thing is that I want to keep the second object always to the left no matter where the main object is.

    I'm thinking of this with a drawing, looking from above (sorry for the bad drawing skills) :


    So if the main object rotates to the right, the object will move forward and rotates a little bit to the right so it's always looking forward, just like the main object. If the main object rotates to the left the second object should move backwards and rotate a little bit to the left too.

    I found this topic
    http://forum.unity3d.com/threads/basic-solar-system-project.63486/

    But the code doesn't work for me (besides I couldn't make it work, lol). It's not exactly a "solar system movement type" what I'm trying to do.

    I have pretty clear the idea of what I want to do, but I don't know how to write it down in code...

    Any suggestions, ideas or clearifications will be appreciated.

    Thanks in advance.
     
  2. AndreasU

    AndreasU

    Joined:
    Apr 23, 2015
    Posts:
    98
    Like this?

    Code (csharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class SecondObject: MonoBehaviour {
    5.  
    6.     public GameObject mainObject;
    7.  
    8.     void FixedUpdate () {
    9.  
    10.         transform.position = mainObject.transform.position + mainObject.transform.rotation * Vector3.right * -3;
    11.         transform.rotation = mainObject.transform.rotation;
    12.     }
    13. }
    14.  
    In the code,
    Code (csharp):
    1. mainObject.transform.rotation*Vector3.right
    2.  
    gives a Vector3 pointing towards the right of the mainObject, relative to its orientation.
     
    ericmartinez likes this.
  3. ericmartinez

    ericmartinez

    Joined:
    Feb 26, 2014
    Posts:
    9

    That's exactly what I wanted. I really appreciate your help. Thanks!