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 position problem!

Discussion in 'Scripting' started by PaxStyle, Jul 16, 2014.

  1. PaxStyle

    PaxStyle

    Joined:
    May 22, 2013
    Posts:
    63
    Hi to all,
    This script moves a straight object on the y axis, but when I incline the object, it don't go anymore on the y axis, but it goes parallel to itself.
    I would like that it goes on the same line of action when it is straight.
    Hope you can help me.

    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. }
     
  2. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
    Use transform.right instead of new Vector3(0, 1, 0)

    transform.right is in local space and you're current code moves in world space.
     
  3. PaxStyle

    PaxStyle

    Joined:
    May 22, 2013
    Posts:
    63
    And how I apply transform.traslate to transform.right?
     
  4. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
    Um - the same way you did with new Vector3() ?
     
  5. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
  6. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
    I doubt that will solve the issue. The other way to do it is to just skip Translate

    Code (csharp):
    1.  
    2. transform.position += transform.right * translation;
    3. transform.position -= transform.right * translation;
    4.  
     
  7. PaxStyle

    PaxStyle

    Joined:
    May 22, 2013
    Posts:
    63
    After 1000 tests, I found the correct way to do this: ;)

    Code (CSharp):
    1. transform.position -= transform.up * translation;
    2.  
    and for add the up and down limits it's complicated? what i have to look?