Search Unity

Moving my object with transform.Translate make it move in wrong way

Discussion in 'Scripting' started by Matriac, Feb 5, 2016.

  1. Matriac

    Matriac

    Joined:
    Nov 12, 2015
    Posts:
    3
    I'm having a hard time with unity trying to translate a simple object. The object move in a 3 dimension world but only on the x and z axis. The function I'm using is the Translate function of my transform of my gameobject. x and z are the position I'm trying to move my object.

    Code (CSharp):
    1. transform.Translate (( new Vector3(x - transform.position.x ,0,z - transform.position.z)).normalized * Time.deltaTime  * speed,Space.World);
    So here's the problem I'm dealing with : If the result of my calcul is the following Vector : (0,0,-1.0), my object move slowly in the wrong direction.

    Example :

    Starting position (25.16, 1.0, 12.0) Final position after the translate function : (25.6, 1.0, 12.1)

    Any help would be appeciate to help me understand this
     
  2. BluHornet

    BluHornet

    Joined:
    Feb 23, 2015
    Posts:
    40
    can you show how x and z are calculated? have you Debug.Log() to check that x and z are actually what you think they are? have you checked using the local view that the game object is facing the correct direction? I have seen models that are facing backwards of their local z axis.
     
  3. Matriac

    Matriac

    Joined:
    Nov 12, 2015
    Posts:
    3
    So the X and Z are calculate this way :
    Code (CSharp):
    1. Vector3 distance = Camera.main.ScreenToWorldPoint (new Vector3 (x, y, Camera.main.nearClipPlane))  ;
    Here the Debug with my programm running.
    upload_2016-2-5_15-51-6.png
    I think the models is facing the right way, I'm not really sure how to check this but i tried to click on Global to put in Local and the axis was the same on my object. As we can see in the debug , my object look like to go in the right way, but after some call here whats happents :
    upload_2016-2-5_15-53-59.png
    My object start moving in the wrong ways.

    EDIT :
    I tried using Vector3.MoveTowards and it seem like the object goes in the right place but he is doing a curve instead of a straight line,

    Vector3.Lerp is also not working properly and it is doing the same thing than transform.Translate.
     
    Last edited: Feb 5, 2016
  4. The-Little-Guy

    The-Little-Guy

    Joined:
    Aug 1, 2012
    Posts:
    297
    Maybe you're looking for transform.forward...

    Vector3 = World Space
    Transform = Local Space

    Code (CSharp):
    1. transform.Translate (transform.forward * Time.deltaTime  * speed);
    There is also:

    Code (csharp):
    1. transform.forward   // = forward
    2. transform.up        // = up
    3. transform.right     // = right
    4. -transform.forward  // = backward
    5. -transform.up       // = down
    6. -transform.right    // = left
     
    BitGamey likes this.
  5. Matriac

    Matriac

    Joined:
    Nov 12, 2015
    Posts:
    3
    I already try this solution,
    after rotating my object with this :
    Code (CSharp):
    1. transform.LookAt(new Vector3(x,1,z));
    I did the translate with forwars like this :
    Code (CSharp):
    1. transform.Translate(transform.forward * Time.DeltaTime * speed);
    The problem I had with this one was the fact that my object was not moving in a straight line but instead in a strange big curve. ( I have no reason what but it is probably relate to the other problems i have) The object was able to reach the right point, but it was doing a curve and sometimes, it was doing an infinite circle around a random point.

    I think theres probably a problem with my object because any way I try to move it, it goes really strange and do not work as expected.
     
  6. BluHornet

    BluHornet

    Joined:
    Feb 23, 2015
    Posts:
    40
    can we see all of your movement and rotation script? Also what type of camera system are you going for? A FPS, TPS over the shoulder? A Top down camera? It looks like you are trying for FPS. The z value looks like it is driven by the near clip plane... Does the character always move forward in this game? To help we need to see from the time you take the inputs to the final output of moving the character. If in both global mode and local mode if the transform gadget is the same your character is aligned with origin. I really think something is wrong with the math before the movement.
     
  7. Kusakubari

    Kusakubari

    Joined:
    Mar 4, 2015
    Posts:
    8
    pivot point?!
     
  8. Suddoha

    Suddoha

    Joined:
    Nov 9, 2013
    Posts:
    2,824
    That still doesn't show how they're calaculated.

    Can you describe exactly how your scene is set up, how your want to move your object and post the complete script?
     
  9. JottoWorol

    JottoWorol

    Joined:
    Nov 16, 2019
    Posts:
    1
    Just use
    transform.position += delta
    and be happy
     
  10. YilinYang

    YilinYang

    Joined:
    Mar 18, 2022
    Posts:
    1
    Hey man, I had the same issue. But eventually I figured it out. In my case, I was trying to move my character in horizonal axis. I changed my character's rotation when moving towards a different direction. And I forgot that actually changed my character's axis's direction. For example, I wanted my character to rotate 180 degrees to face left while moving towards left, then my character's axis would rotate 180 degrees too which means now forward is pointing backwards. The way I fixed it was to set my character as a child object as an empty object and it solves the problem.