Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice

Space Ship Code

Discussion in 'Scripting' started by KlaRo115, Aug 22, 2008.

  1. KlaRo115

    KlaRo115

    Joined:
    Feb 24, 2006
    Posts:
    675
    Hi, Today, I wanted to write a code for space ships, the player must push the forward arrow to move the ship and push space for afterburn...
    But nothing happes, there are only some error messages and I don't know what to do now... :?


    Code (csharp):
    1. var speed = 6.0;
    2. var jumpSpeed = 12.0;
    3.  
    4. private var moveDirection = Vector3.zero;
    5.  
    6. function FixedUpdate() {
    7.  
    8.         // We are grounded, so recalculate movedirection directly from axes
    9.         moveDirection = new Vector3(Input.GetAxis("Vertical"));
    10.         moveDirection = transform.TransformDirection(moveDirection);
    11.         moveDirection *= speed;
    12.        
    13.         if (Input.GetButton ("Jump")) {
    14.             moveDirection.z = jumpSpeed;
    15.         }
    16.  
    17.     // Apply gravity
    18.     moveDirection.y = Time.deltaTime;
    19.    
    20.     // Move the controller
    21.     var flags = transform.Translate(moveDirection * Time.deltaTime);
    22. }
     

    Attached Files:

  2. bigkahuna

    bigkahuna

    Joined:
    Apr 30, 2006
    Posts:
    5,434
    Code (csharp):
    1. moveDirection = new Vector3(Input.GetAxis("Vertical"));
    Here's at least part of the problem: "Vector3" requires values for all three axis, so something like this might work:

    Code (csharp):
    1. moveDirection = new Vector3(Input.GetAxis("Vertical"), y, z));
    Haven't tried this but it should at least get you started.
     
  3. joe gamble

    joe gamble

    Joined:
    Jan 3, 2007
    Posts:
    85
    try this:


    var speed = 6.0;
    var jumpSpeed = 12.0;

    private var moveDirection = Vector3.zero;

    function FixedUpdate() {

    // We are grounded, so recalculate movedirection directly from axes

    // Vector3 requires 3 arguments
    moveDirection = new Vector3(0,Input.GetAxis("Vertical"),0);

    moveDirection = transform.TransformDirection(moveDirection);
    moveDirection *= speed;

    if (Input.GetButton ("Jump")) {
    moveDirection.z = jumpSpeed;
    }

    // Apply gravity
    moveDirection.y = Time.deltaTime;

    // Move the controller

    //transform.Translate doesn't return a value
    transform.Translate(moveDirection * Time.deltaTime);
    }
     
  4. KlaRo115

    KlaRo115

    Joined:
    Feb 24, 2006
    Posts:
    675
    Thanks guys, that's working great! :)