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.

Question What Variable Stores Object Direction?

Discussion in 'Scripting' started by ateryubone, Nov 28, 2022.

  1. ateryubone

    ateryubone

    Joined:
    Nov 28, 2022
    Posts:
    15
    If I have an object in my object hierarchy, called Cylinder.

    And I use C# to move the object forward like this:

    transform.position += transform.forward * Time.deltaTime;


    It will always move in one direction, that is transform.forward. But I want this direction to be dynamic so that I can change it in real time. I could continue using transform.forward if there was a way to change the object's direction. For example I want to align its direction with another object "otherobject" as it moves: Like this:

    Cylinder.transform.direction = otherobject.transform.direction;

    Then the value of Cylinder.transform.forward would change to always match that of otherobject, so I wouldn't need to make it any more complicated than that if possible. I'm just wondering how to GET the value of transform.forward and how to CHANGE the value of transform.forward so that it matches the transform.forward of another object. For example, what if I wanted to make "Cylinder" move in the same direction as another object that is turning randomly? How would I link the two objects' directions? Let me know if my questions were unclear.
     
  2. ateryubone

    ateryubone

    Joined:
    Nov 28, 2022
    Posts:
    15
    Actually I partially resolved it on my own already:

    I just had to create a Transform called example and then do this.
    transform.forward = example.forward;

    I'll leave this up in case anybody else gets the same issue later. It doesn't work for the z-coordinate, that is 'height' so I'll need to figure that out I guess.
     
  3. ateryubone

    ateryubone

    Joined:
    Nov 28, 2022
    Posts:
    15
    Ok, I figured that out too now.
    Although maybe it's not ideal I just made a static float representing the direction in the Y axis (height) and then I just do
    transform.position += new Vector3(0, script_name.static_variable, 0);
    And I just edit the static variable in the object I'm trying to mirror.
    And that works so I think I answered my own question.
    But if anybody else has the same issue later here is how to solve it.

    If anybody has any better solutions to do what I'm trying to do then please contribute... thank you!!!!
     
    Last edited: Nov 28, 2022
  4. Owen-Reynolds

    Owen-Reynolds

    Joined:
    Feb 15, 2012
    Posts:
    1,766
    The direction an object faces is transform.rotation. The standard game way to aim yourself at some other object (or just some point you have) is
    transform.LookAt(target)
    .

    transform.forward=example.forward;
    makes you look in the same direction as them, but not at the same spot. For example, these are all looking up: ^ ^ ^ ^, but each is looking at a different letter. This slash / and this other slash over here / are facing the same direction, but definitely not aimed at the same thing.