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

Need help with translate vector not working.

Discussion in 'Scripting' started by Carl_Slate, Jun 23, 2021.

  1. Carl_Slate

    Carl_Slate

    Joined:
    May 27, 2020
    Posts:
    12
    Hello. I am trying to work out a way to raycast a distance the length of a hypotenuse. For help visualizing the trigonometry of this, I set up a sphere to rotate to a subject angle, and translate forward the distance of the secant. My objective at this point is simply to get the sphere's resulting position to move vertically while the subject angle it is translating along changes. If it works, then I think I should just have to use this secant value as my raycast distance in the properly composed version. However, I've bumped into this weird problem where transform.Translate(transform.forward) is not making the sphere translate forward, but it is instead translating diagonally.

    Here is the code I am using:
    Code (CSharp):
    1. float myCosine = 0f;
    2. float myAngle = 0f;
    3. Transform visPos;
    4.  
    5.     void Start()
    6.     {
    7.         visPos = GameObject.Find("CosinePos").transform;    // This is the sphere I am using to visualize the far-end of the hypotenuse.
    8.         myAngle = transform.eulerAngles.x;
    9.     }
    10.  
    11.     void Update()
    12.     {
    13.        if (myAngle != transform.eulerAngles.x)
    14.        {
    15.             // Get new cosine from radians
    16.             myCosine = Mathf.Cos(transform.eulerAngles.x * 0.017455f);
    17.  
    18.             myAngle = transform.eulerAngles.x;    // Remember angle to myAngle
    19.  
    20.            // Match visual position and rotation to this position and rotation.
    21.             visPos.position = transform.position;
    22.             visPos.rotation = transform.rotation;
    23.  
    24.             // Translate visual forward the distance of secant
    25.             visPos.Translate(visPos.forward * (1 / myCosine));      
    26.        }
    27.     }
    Can anyone help me figure out why my transform.forward is not translating forward, or otherwise how to achieve translating forward here?
    Translate_Help.gif

    I have serialize-field-ed the cosine and secant to confirm that they are calculating correctly, and they are. (Both to four decimal positions of precision, which is adequate.)
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,971
    Don't compare floating point values for equality (or inequality), due to floating point imprecision.

    Instead, use Mathf.Approximately() if you understand the implicit epsilon term, or implement your own "close enough" compare.
     
  3. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,735
    Code (CSharp):
    1. visPos.Translate(visPos.forward * (1 / myCosine));  
    Transform.Translate expects a Vector3 in local space but you are passing in
    visPos.forward
    which is a world space vector. To fix either pass in a local space vector, or add the second parameter to specify that you're passing in a world space vector:
    Code (CSharp):
    1. visPos.Translate(Vector3.forward * (1 / myCosine));  
    or
    Code (CSharp):
    1. visPos.Translate(visPos.forward * (1 / myCosine), Space.World);  
     
  4. Carl_Slate

    Carl_Slate

    Joined:
    May 27, 2020
    Posts:
    12
    PreatorBlue That nailed it! Apparently I was misunderstanding what the ".Translate" does with the vector you give it. I changed it to Vector3.forward and I am now getting exactly what I wanted. I understand better now. What a relief! Thank you very much for your help.