Unity Space Shooter Tutorial: How do I actually "move" the player?

Hi guys, I've been doing the Unity "Space Shooter" tutorial.  (Not the CG Cookie Version) 

https://www.youtube.com/watch?v=rVSLczG1M1E&list=PLX2vGYjWbI0RibPF7vixmr4x8ONJX-mNd&index=6

After trying to figure out the "Get Component" Stuff (As the tutorial was based on the Previous Unity and I needed to "Get Component" in order to Use Unity 5) ...I think I've achieved some success.


Basically, I copied and pasted the following in MonoDevelop. 



using UnityEngine;
using System.Collections;

public class PlayerController : MonoBehaviour {

    private Rigidbody rb;

    public float speed;
    public float tilt;
    public Boundary boundary;

    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 * speed);

        Vector3 bounds= new Vector3 ((Mathf.Clamp(rb.position.x, boundary.xMin, boundary.xMax)), 0.0f, (Mathf.Clamp(rb.position.z, boundary.zMin, boundary.zMax)));

        rb.position = bounds;

        rb.rotation = Quaternion.Euler (0.0f, 0.0f, (rb.velocity.x * -tilt));
    }
}

[System.Serializable]
public class Boundary {

    public float xMin, xMax, zMin, zMax;

}



However, after I went back to the game and pressed "Play"  I find that I'm not able to move my character around as it is done on screen. 


Do I use the arrow keys? Do I press some buttons?

Or does this require an extra script entirely to be able to move the space-ship in Game Mode? 



  • Jonathan Gonzalez(jgonzalez) replied

    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. 

  • gauntlit replied

    Hi John, 

    Thanks for your replay...I've tried the initial scrip with your advice applied. 

    "This script is applied to the object that has a rigid body....I tried this myself, and while it didn't move like a spaceship it did move my sphere around with a rigidbody applied."

    ...

    Please correct me if I'm wrong here. But looking at my Unity Layout, it seems that a Rigidbody is indeed applied to my spaceship.

    As well, the script "PlayerController" is also counted as a component...Although it DOES seem to be faded out, does this mean the script does not apply to the space-ship? 


    How do I apply the script to the spaceship? 

    Is my problem that the script is entitled "PlayerController" whereas the ship itself is entitled vehicle_playerShip? 

    (I tried to re-name the script as vehicle_playerShip...But no luck, it would not run.) 



    I then pressed the "Play" button... It seems by your comment that you were suggesting the Rigidbody would apply to the "sphere" and therefore the "sphere" would move around.

    Is the "Sphere" represented by the small icon which the yellow arrow points to? 

    I pressed "Play" and tried to press the arrow and WASD keys..But it does not seem that the sphere moved. (Or perhaps it DID move and it's invisible?) 




    Anyway, I tried to erase this script entirely and replace it with the simplified script you gave me...However the response from the Console is 

    "The Referenced Script on this Behaviour (Game Object 'vehicle_playerShip') is missing! 


    Basically, at this point I'm just trying to figure out how to press the arrow keys and move the ship around. 


  • Jonathan Gonzalez(jgonzalez) replied

    Based off your screenshot you didn't increase the speed value. So even if you were trying to move around you're not moving the object around because it thinks you want to move with a speed of 0. 

    The error you're getting by adding the simplified version of the script is probably because you have another component/script that relies on that script. So when you removed it, it doesn't know where to find it. 

  • gauntlit replied

    Thanks!  Yeah, I finally got it to work!  (I also had to change the Boundary)