Search Unity

Question on RigidBody velocity from Space Shooter tutorial

Discussion in 'Community Learning & Teaching' started by Freerider, Mar 21, 2015.

Thread Status:
Not open for further replies.
  1. Freerider

    Freerider

    Joined:
    Mar 21, 2015
    Posts:
    2
    The first coding step in the tutorial says to write this code, but when I do, RigidBody does not contain any velocity components. However, when I look at the RigidBody declaration, it's full of velocity parameters. Anyone know why this is?

    using UnityEngine;
    using System.Collections;

    public class PlayerController : MonoBehaviour
    {
    void FixedUpdate()
    {
    float moveHorizontal = Input.GetAxis ("Horizontal");
    float moveVertical = Input.GetAxis ("Vertical");


    Rigidbody.velocity

    }
    }
     
  2. Freerider

    Freerider

    Joined:
    Mar 21, 2015
    Posts:
    2
    I figured it out. Need to call GetComponent<Rigidbody>().velocity for some reason. Doesn't work without GetComponent.
     
  3. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    Yes. This is a change between Unity 4 and Unity 5. This reference should be cached for best performance.

    There is an official thread in the sticky section of this section.

    (P.S.: please format your code!)
     
  4. n0vice

    n0vice

    Joined:
    Apr 8, 2015
    Posts:
    4
    I'm a complete novice and I'm following exact same steps but getting another error:

    MissingComponentException: There is no 'Rigidbody' attached to the "Main Camera" game object, but a script is trying to access it.
    You probably need to add a Rigidbody to the game object "Main Camera". Or your script needs to check if the component is attached before using it.
    UnityEngine.Rigidbody.set_velocity (Vector3 value)
    PlayerController.FixedUpdate () (at Assets/Scripts/PlayerController.cs:12)

    ------
    Please note that I have only one Rigidbody component, and it is attached to the "Player" game object.
    ------

    my code is
    ------

    using UnityEngine;
    using System.Collections;
    public class PlayerController : MonoBehaviour
    {
    void FixedUpdate()
    {
    float moveHorizontal = Input.GetAxis ("Horizontal");
    float moveVertical = Input.GetAxis ("Vertical");
    Vector3 movement = new Vector3 (moveHorizontal, 0.0f, moveVertical);
    GetComponent<Rigidbody>().velocity = movement;
    }
    }

    -------

    Please help.
     
    Last edited: Apr 8, 2015
  5. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    That error says you are trying to access a Rigidbody on the camera component? Is the script on the wrong GameObject? Shouldn't this be on the player GameObject?

    For more information on this project, please use the official thread:
    http://forum.unity3d.com/threads/space-shooter-tutorial-q-a.313899

    (P.S.: Please learn to format your code!)
     
Thread Status:
Not open for further replies.