Search Unity

Need help with Moving objects

Discussion in 'Scripting' started by mimishak, Dec 4, 2012.

  1. mimishak

    mimishak

    Joined:
    Dec 2, 2012
    Posts:
    9
    Hi im a beginner to Unity3d and i need help with moving object when pressed a button. just take a look at my codings.

    //Moving Object Forward
    if (Input.GetAxis("Up")) {
    transform.Translate(Vector3.forward, Space.Self);
    }
    }

    //Rotating Object Leftwards
    if (Input.GetAxis("Left")) {
    transform.Rotate(Vector3(30 * Time.deltaTime, 0, 0), Space.Self);
    }
    }

    The movement forward is fine. but after i rotate, the movement is not forward and the player is moving to the standard axis, not the axis of the player. Help needed. I'm a beginner so please help me! And why isn't the Space.Self working?
     
  2. MicroEyes

    MicroEyes

    Joined:
    Jul 3, 2012
    Posts:
    309
    Use this Code:

    Code (csharp):
    1.  
    2. // Rotate around y - axis
    3.     transform.Rotate(0, Input.GetAxis ("Horizontal") * rotateSpeed, 0);
    4.    
    5.     // Move forward / backward
    6.     var forward : Vector3 = transform.TransformDirection(Vector3.forward);   [B] //You Missed this Line[/B]
    7.     var curSpeed : float = speed * Input.GetAxis ("Vertical");
    8.     transform.Translate(forward * curSpeed);
    9.