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

transform.Translate problem

Discussion in 'Scripting' started by PaxStyle, Jun 24, 2014.

  1. PaxStyle

    PaxStyle

    Joined:
    May 22, 2013
    Posts:
    63
    Hi!
    I made this script for move a transverse piston, so I used transform.traslate, but the piston dosn't move Up and Down, but transversely.
    This pic shows how the piston should move:



    This is how piston moves (wrong)



    This is the code:

    Code (JavaScript):
    1. var move : float = 0.1;
    2. var translation : float;
    3. function Start () {
    4. }
    5. function Update () {
    6. if(Input.GetKey(KeyCode.A)) {
    7. translation = Time.deltaTime * move;
    8. transform.Translate(new Vector3(0,1,0) * translation);
    9. }
    10. if(Input.GetKey(KeyCode.S)) {
    11. translation = Time.deltaTime * move;
    12. transform.Translate(new Vector3(0,-1,0) * translation);
    13. }
    14. }
    Thank you for your support. :)
     
  2. DanielQuick

    DanielQuick

    Joined:
    Dec 31, 2010
    Posts:
    3,137
  3. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,398
    Keep Translate and use Space.World.

    --Eric
     
  4. PaxStyle

    PaxStyle

    Joined:
    May 22, 2013
    Posts:
    63
    I tried this but the piston doesn't moves like the firts image that I posted. But comes out its position.
     
  5. aniv

    aniv

    Joined:
    Jan 19, 2011
    Posts:
    135
    Is piston model rotated? Anyway, you can always use Vector3.Lerp to interpolate between start and end position linearly.
     
  6. JamesLeeNZ

    JamesLeeNZ

    Joined:
    Nov 15, 2011
    Posts:
    5,616
    Are the pivot/rotation set up correctly in the model?
     
  7. PaxStyle

    PaxStyle

    Joined:
    May 22, 2013
    Posts:
    63
    Looking this image the pivot seems positioned right, no?

     
  8. aniv

    aniv

    Joined:
    Jan 19, 2011
    Posts:
    135
    What does it say in top left corner of Unity. Pivot or Center and Local or Global? Set it to local and see if any of the axis is parallel to the piston.