Umm okay so basically I’m working on 2D game, and I’ve only just started using Unity. For about a week or so. So now I’m stuck. I’ve written some code and it goes like this:
void FixedUpdate ()
{
float move = Input.GetAxis (“Horizontal”);
rigidbody2D.velocity = new Vector2(move * maxSpeed, rigidbody2D.velocity.y);
}
So basically the problem is that “velocity” is coloured red. And the console is telling me that velocity is not defined. I’ve looked up a lot of stuff in here but nothing really tells me what exactly I need to type to define something. Please help!
The field “rigidbody2d” of the Component class is deprecated. If you want to access a component on a GameObject, you need to get a reference to the component. You do this with the “GetComponent()” method from the Component class. Since MonoBehaviour is a class which inherits from the Component class (meaning that it has all of the functionality of a Component), you can use this method on any MonoBehaviour class.
private void FixedUpdate()
{
float move = Input.GetAxis("Horizontal");
Rigidbody2D rb2D = this.GetComponent<Rigidbody2D>();
rb2D.velocity = new Vector2(move * this.maxSpeed, rb2D.velocity.y);
}