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
  4. Dismiss Notice

Compute translation vector

Discussion in 'Scripting' started by wwu39, Apr 7, 2021.

  1. wwu39

    wwu39

    Joined:
    Nov 21, 2018
    Posts:
    25
    This is a 2D game and everything has y=0. I want to move a GameObject from position A to position B using Transform.Translate and has some limits on the translation. But I have problem on how to compute the translation vector.
    Code (CSharp):
    1. public void MoveToUsingTranslation(Vector3 targetPos)
    2.     {
    3.         Vector3 translation = Vector3.zero;
    4.         // translation = ???
    5.         translation.x = Mathf.Clamp(translation.x, -1500, 1500);
    6.         translation.y = 0; // 2d movement
    7.         translation.z = Mathf.Clamp(translation.y, -1500, 1500);
    8.         transform.Translate(translation);
    9.     }
     
    Last edited: Apr 7, 2021
  2. GroZZleR

    GroZZleR

    Joined:
    Feb 1, 2015
    Posts:
    3,201
  3. wwu39

    wwu39

    Joined:
    Nov 21, 2018
    Posts:
    25
  4. arfish

    arfish

    Joined:
    Jan 28, 2017
    Posts:
    777
  5. wwu39

    wwu39

    Joined:
    Nov 21, 2018
    Posts:
    25
    Well I don't want to rotate it