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

Get position relative to transform.forward

Discussion in 'Scripting' started by invisage, Mar 4, 2022.

  1. invisage

    invisage

    Joined:
    Jun 27, 2019
    Posts:
    23
    Not sure if this is the right spot.

    Moving an object to a random spot and in a random direction.
    Have a second object that starts in a relative position to the first.
    It cant be parented etc so this has to be done in code.
    I have reference to the position of the second object.
    I need to get have the second object maintain its relative position to the first, relative to the first objects forward vector.
    eg Second object is up and to the left at the start, whenever object one changes position, object two needs to always be up and to the left of object one.

    I know how to get direction and magnitude of the second object at the start, but just cant figure out how to apply it to the first objects new transform.forward.

    Any ideas would be appreciated.
     
  2. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,507
    I presume you're not talking about any Physics components here (2D or 3D) but just Transforms? If so then no, it's not a Physics thing and should be posted on the Scripting forum.

    I can move your post for you if you wish.
     
  3. invisage

    invisage

    Joined:
    Jun 27, 2019
    Posts:
    23
    Yes please and thanks
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,710
    I'm not sure about precisely what you want, but let me drop some tidbits on you to perhaps guide you to success.

    Every Transform has some shortcuts as you note, such as
    transform.forward
    and
    transform.right


    If I am sitting in your scene and I want to spawn something ahead of me 5 meters and to my right 2 meters, here would be how I would do that:

    Code (csharp):
    1. // Drag the Kurt object in here
    2. public Transform Kurt;
    3.  
    4. // set these up with values you want, using the inspector
    5. public float ahead = 5.0f;
    6. public float right = 2.0f;
    Now we place something 5 meters ahead of Kurt and 2 meters to his right.

    Code (csharp):
    1. Vector3 KurtPosition = Kurt.position;
    2.  
    3. Vector3 spawnPosition = KurtPosition +
    4.     Kurt.forward * ahead +
    5.     Kurt.right * right;
    6.  
    7. // TODO: use spawnPosition to make your thing
    8. GameObject thing = Instantiate<GameObject>( ThingPrefab, spawnPosition, Kurt.rotation);
    That third term will also rotate the object to match Kurt's rotation.
     
    invisage likes this.
  5. invisage

    invisage

    Joined:
    Jun 27, 2019
    Posts:
    23
    Thanks Kurt, that was the missing piece. I had figured I had to break down the vector, and that the forward direction was one component. I didn't think of using transform.right (and in this instance transform.up for 3d Vector). The rest was easy then:

    upload_2022-3-7_6-1-19.png