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

Translate, ignoring the rotation.

Discussion in 'Scripting' started by Onilut, May 30, 2017.

  1. Onilut

    Onilut

    Joined:
    Aug 10, 2015
    Posts:
    36
    Code (CSharp):
    1. if (direccionBorrado == 0)
    2.             {
    3.                 this.transform.Translate(-.25f * Time.deltaTime, 0, 0, relativeTo:Space.World);
    4.                 this.transform.Rotate(0,0,.25f);
    5.             }
    6.             else if (direccionBorrado == 2)
    7.             {
    8.                 this.transform.Translate(.25f * Time.deltaTime, 0, 0, relativeTo:Space.World);
    9.                 this.transform.Rotate(0,0,-.25f);
    10.             }
    I want to translate the gameobject to right or left (depending of "direccionBorrado" value), i also want that the gameobject rotates slightly... but when I do, the translate make the gameobjects move to their new x axis (due to the rotation)... I added the "relativeTo:Space.World" to translate but it seems it does not work... What am I missing?
     
    chris15smile likes this.
  2. Scabbage

    Scabbage

    Joined:
    Dec 11, 2014
    Posts:
    268
    Code (CSharp):
    1. transform.position += new Vector3(0.25f * Time.unscaledDeltaTime, 0, 0);
     
  3. Onilut

    Onilut

    Joined:
    Aug 10, 2015
    Posts:
    36
    I tried it...
    Code (CSharp):
    1. transform.position += new Vector3(0.25f * Time.unscaledDeltaTime, 0, 0);
    it does exactly the same thing as
    Code (CSharp):
    1. transform.Translate(.25f * Time.deltaTime, 0, 0, Space.World);
    it is still affected for the rotation... and it does not make a constant movement to the left/right
     
  4. larku

    larku

    Joined:
    Mar 14, 2013
    Posts:
    1,422
  5. Antony-Blackett

    Antony-Blackett

    Joined:
    Feb 15, 2011
    Posts:
    1,772
    Translate in world space and transform.position shouldn't be effected by the objects rotation, something else is going on here. Do you have the camera attached as a child to this object? maybe it's just your perspective that is changing?
     
  6. Onilut

    Onilut

    Joined:
    Aug 10, 2015
    Posts:
    36
    My camera isn't attached to anything: https://gyazo.com/4b41c5e6f456254b6e2665176bd78fea

    the thing is, if i comment the rotate line... it makes the movement correctly... So i'm really confused...

    https://gyazo.com/d35f9f98faacf6c66f228bd31d2c899a
     
  7. larku

    larku

    Joined:
    Mar 14, 2013
    Posts:
    1,422
  8. Onilut

    Onilut

    Joined:
    Aug 10, 2015
    Posts:
    36
  9. Laperen

    Laperen

    Joined:
    Feb 1, 2016
    Posts:
    1,065
    As @Antony-Blackett tried to explain, nowhere in the snipplet you have provided tries to move an object relative to the rotation, so the problem is probably coming from somewhere else.
     
    larku likes this.
  10. JoshuaMcKenzie

    JoshuaMcKenzie

    Joined:
    Jun 20, 2015
    Posts:
    897
    could it be that you are transforming in world space, but the object mesh your seeing is a child with a local position?
    comment out those specific lines and confirm if its still translating at all.

    What is the transform hierarchy of the GameObject and which one has the script? get us a picture of that.
     
  11. Onilut

    Onilut

    Joined:
    Aug 10, 2015
    Posts:
    36
    Sorry for late response.

    The game object isn't attached to anything (the script is attached to the prefab tho).

    The gameobject isn't in the scene before starting the game... when the game starts... it loads some gameobjects depending on the number of a variable (Basically... The gameobjects which i'm having the problem is made during runtime).

    You can see them in the follwing link (named "Cubos"): https://gyazo.com/6fdd2b7b8fec688a166350b973aabe3b

    These are the components of the gameopbject:
    https://gyazo.com/9529a1dc125b0d6b39c328165c78a3d0

    Which commenting... i didn't understand what i had to comment exactly...if you mean about the rotate line, if i comment that... the movement is made correcly
     
  12. JoshuaMcKenzie

    JoshuaMcKenzie

    Joined:
    Jun 20, 2015
    Posts:
    897
    Comment the translation (code that is changing the position), not necessarily the rotation (you can comment it too but thats not what we are focusing on). The expected result is that you don't want it to translate when you comment out that code. if the object is still translating even with the comments out then something else is moving the object locally and thats your issue.

    I'm with the others on this one. the problem isn't in the code you posted to us. something else is moving it.
     
    Onilut likes this.
  13. Onilut

    Onilut

    Joined:
    Aug 10, 2015
    Posts:
    36
    This is the complete part of code that makes the movement... if i comment the translate inside the "if", it moves the X axis

    Code (CSharp):
    1. if (failed)
    2.         {
    3.             speed -= 0.2f;
    4.             this.transform.Translate(0, speed * Time.deltaTime, 0);
    5.  
    6.  
    7.             if (this.transform.position.y < gameBotLine)
    8.                 Destroy(this.gameObject);
    9.             //float step = .25 * Time.deltaTime;
    10.             if (removeOrientation == 0)
    11.             {
    12. //                this.transform.position += new Vector3(-asd * Time.unscaledDeltaTime,0,0);
    13. //                this.transform.position = Vector3.MoveTowards(this.transform.position,new Vector3(
    14. //                    this.transform.position.x - 50,this.transform.position.y,this.transform.position.z),.25f * Time.deltaTime);
    15.                 //this.transform.Translate(-.25f * Time.deltaTime, 0, 0, Space.World);
    16.                 this.transform.Rotate(0,0,.25f);
    17.             }
    18.             else if (removeOrientation == 2)
    19.             {
    20. //                this.transform.position += new Vector3(asd * Time.unscaledDeltaTime,0,0);
    21. //                this.transform.position = Vector3.MoveTowards(this.transform.position,new Vector3(
    22. //                    this.transform.position.x + 50,this.transform.position.y,this.transform.position.z),.25f * Time.deltaTime);
    23.                 //this.transform.Translate(.25f * Time.deltaTime, 0, 0, Space.World);
    24.                 this.transform.Rotate(0,0,-.25f);
    25.             }
    26.         }
    So i didn't realize that the first translate wasn't in spaceworld... so they move in the y Axis using the gameobject as reference... and that was the core of my problem... What a dumb problem... I focused that the mistake was located in that condition.

    Thanks!
     
    flaviolucio10 likes this.