Reply To: Unity Space Shooter Tutorial: How do I actually “move” the player?

Forum Archive Unity Space Shooter Tutorial: How do I actually “move” the player? Reply To: Unity Space Shooter Tutorial: How do I actually “move” the player?

#32074
Jonathan Gonzalez
Participant

    This script is applied to the object that has a rigidbody. Check your console for any errors or warnings. I tried this myself, and while it didn’t move like a spaceship it did move my sphere around with a rigidbody applied. The controls use the WASD or arrow keys. With the horizontal/vertical axis it can use any inputs that utilize those axis’. You could also use a joystick for movement since that is built in with those as well. Someone on that video posted a simplified version of the controller that could work as well:

    using UnityEngine;
    using System.Collections;

    public class Movement : MonoBehaviour {

    public Rigidbody rb;

    void Start()
    {
    rb = GetComponent<Rigidbody> ();
    }

    void FixedUpdate ()
    {
    float moveHorizontal = Input.GetAxis (“Horizontal”);
    float moveVertical = Input.GetAxis (“Vertical”);

    Vector3 movement = new Vector3 (moveHorizontal, 0.0f, moveVertical);
    rb.velocity = movement;
    }
    }

    I’m not sure if the video uses the Z axis to move in and out, but you can change this line to this:

    Vector3 (moveHorizontal, 0.0f, moveVertical);

    to 

    Vector3 (moveHorizontal, moveVertical, 0);

    That way you can move along the X and Y axis instead of the X and Z axis.