rigidbody2D's velocity not working
I'm trying to make a 2d object move around freely but I keep receiving the error with my code: error CS0120: An object reference is required to access non-static member `UnityEngine.Rigidbody2D.velocity' . I'm not certain what's causing it or how to really fix it, code below if anyone can help me understand why it is my rigid body doesn't want to move.
using UnityEngine;
/// <summary>
/// Player controller and behavior
/// </summary>
public class PlayerScript : MonoBehaviour
{
/// <summary>
/// 1 - The speed of the ship
/// </summary>
public Vector2 speed = new Vector2(50, 50);
// 2 - Store the movement
private Vector2 movement;
void Update()
{
// 3 - Retrieve axis information
float inputX = Input.GetAxis("Horizontal");
float inputY = Input.GetAxis("Vertical");
// 4 - Movement per direction
movement = new Vector2(
speed.x * inputX,
speed.y * inputY);
}
void FixedUpdate()
{
// 5 - Move the game object
Rigidbody2D.velocity = movement;
}
}
For more information I'm attempting to follow this tutorial that was written for unity 4.3 and I am currently using unity 5 if that helps.
Answer by MelvMay · Dec 04, 2015 at 07:50 AM
First, you should try using a Google search for 'CS0120' as it comes up with the answer many times, including posts on the Unity site instead of posting here.
The error tells you exactly what is wrong so I presume you're new to programming? If so, the 'Rigidbody2D' you're using is the type name. The only time you can access properties like this is if they are static; velocity isn't static. You want the instance of the Rigidbody2D component you've presumably added. You use:
var rb = GetComponent<Rigidbody2D>();
to get the component. You can then access its properties like this
rb.velocity = movement;
I am new to program$$anonymous$$g and for some reason didn't think to google the error code so thank you. What is the rb variable though, just the name of it?
'rb' is the name of the variable I chose that stands for 'rigid-body'; could be anything.
If I were to rewrite your code, I'd do it like so: using UnityEngine;
/// <summary>
/// Player controller and behavior
/// </summary>
public class PlayerScript : $$anonymous$$onoBehaviour
{
/// <summary>
/// 1 - The speed of the ship
/// </summary>
public Vector2 speed = new Vector2(50, 50);
// 2 - Store the movement
private Vector2 movement;
private Rigidbody2D body;
void OnStart()
{
body = GetComponent<Rigidbody2D>();
}
void Update()
{
// 3 - Retrieve axis information
float inputX = Input.GetAxis("Horizontal");
float inputY = Input.GetAxis("Vertical");
// 4 - $$anonymous$$ovement per direction
movement = new Vector2(
speed.x * inputX,
speed.y * inputY);
}
void FixedUpdate()
{
// 5 - $$anonymous$$ove the game object
body.velocity = movement;
}
}
Hope this helps.
It does, a lot. So the problem was that there was no rigid body to call on, and you fixed that by first creating the private method(?) of Rigidbody2D and na$$anonymous$$g it body, then when the object is first called on, it retrieves the information for a rigidbody2D. Does that sound right?
Your answer
Follow this Question
Related Questions
Why is a gameobject moves at start? 0 Answers
how to fix velocity for jump and movement or fall in 2d pltform game with gravity 4 Answers
how to clamp( set max value) the velocity of a 2d rigidbody? 1 Answer
How do I make my character move on the Z axis on 2d rigidbody? 0 Answers
How to have the same speed on Rigidbody2D in all resolutoins ?? 0 Answers