- Home /
Having trouble with simple 2D movement
Hello Everybody,
I'm quite a noob at scripting, and for days i've been working on this script to make my character move and jump (2D platformer game). After long hours of understanding the errors i got, there is only one left, and i'm breaking on it.
I got the script from several tutorials, customising it by myself, and here is the erro message i get :
"Assets/Scripts/PlayerScript.cs(43,29): error CS0120: An object reference is required to access non-static member `UnityEngine.Rigidbody2D.AddForce(UnityEngine.Vector2)'"
The point that confuses me is that the error is talking about Vector2, whereas i only mention Vector3 in my script. I assume that there has to be a little essential thing that ican't see, maybe you could say me what.
I put you my code Thanks by advance!
using UnityEngine;
using System.Collections;
/// <summary>
/// Player controller and behavior
/// </summary>
public class PlayerScript : MonoBehaviour
{
public int horizontalSpeed = 1;
public int verticalSpeed = 1;
public int rotationSpeed = 20;
public int jumpSpeed = 100;
public float gravity = 14.0F;
private Vector3 moveDirection = Vector3.zero;
private Rigidbody2D controller;
void Start ()
{
controller = gameObject.GetComponent<Rigidbody2D> ();
}
void Update()
{
// fully control character
float horizontalDirection = Input.GetAxis("Horizontal") * horizontalSpeed;
float forwardDirection = Input.GetAxis("Vertical") * verticalSpeed;
moveDirection = new Vector3(horizontalDirection, 0, forwardDirection);
// get jump event
if (Input.GetKey(KeyCode.Space))
{
moveDirection.y = jumpSpeed;
}
Rigidbody2D.AddForce(Vector3.up * Time.deltaTime);
}
}
Answer by robertbu · Jan 20, 2014 at 11:12 PM
Your problem is on line 43. You are using the Rigidbody2D class and trying to call AddForce(). Given the rest of your code, this line should be:
controller.AddForce(Vector3.up * Time.deltaTime);
Note if the mass of your object is the default 1.0, you may have to increase the force applied:
controller.AddForce(Vector3.up * Time.deltaTime * forceFactor);
But even better, would be to get rid of the deltaTime, and call this inside of FixedUpdate():
void FixedUpdate() {
controller.AddForce(Vector3.up * forceFactor);
}
robertbu is right, try to do as much physics as possible inside FixedUpdate()
Answer by Exalia · Jan 20, 2014 at 11:07 PM
Use
controller.AddForce(Vector3.up * Time.deltaTime);
Instead of
Rigidbody2D.AddForce(Vector3.up * Time.deltaTime);
The reason you were getting an error is because Rigidbody2D is a type, controller is the variable, you need to access a declared rigidbody to use AddForce();
You declared it at the start as controller
I hope this helps, let me know how you get on
Answer by Vignalou · Jan 21, 2014 at 09:23 AM
Thank you guys. The scripts had no errors with Exalia's solution, i realised i did'nt put the if function so my character will move with right and left arrow. I added the function, and now my script has no fatal error, but my character is not moving at all. An idea of what's going wrong in here?
using UnityEngine;
using System.Collections;
/// <summary>
/// Player controller and behavior
/// </summary>
public class PlayerScript : MonoBehaviour
{
public int horizontalSpeed = 1;
public int verticalSpeed = 1;
public int rotationSpeed = 20;
public int jumpSpeed = 100;
public float gravity = 14.0F;
private Vector2 moveDirection = Vector2.zero;
private Rigidbody2D controller;
void Start ()
{
controller = gameObject.GetComponent<Rigidbody2D> ();
}
void Update()
{
// fully control character
if (Input.GetKey (KeyCode.RightArrow))
{
float horizontalDirection = Input.GetAxis ("Horizontal") * horizontalSpeed;
moveDirection = new Vector2 (horizontalDirection, 0);
}
if (Input.GetKey (KeyCode.LeftArrow))
{
float horizontalDirection = Input.GetAxis ("Horizontal") * horizontalSpeed;
moveDirection = new Vector2 (horizontalDirection, 0);
}
// get jump event
if (Input.GetKey(KeyCode.Space))
{
moveDirection.y = jumpSpeed;
float forwardDirection = Input.GetAxis("Vertical") * verticalSpeed;
}
}
void FixedUpdate ()
{
controller.AddForce (Vector2.up * Time.deltaTime);
}
}
Your code is difficult to understand without a context, can you maybe provide some screenshots or comments in your code with what you 'think' should be happening?
Yes, sure. So its a basic platformer game, with a character and aliens, and A$$anonymous$$47. I want my character to be able to move letf/right with arrow keys, and jump with space key. On the right side, you can see the platforms that constitue the landscape of the game. I Need the character to be able to jump on it, cause they are moving platforms that permits to go up on the level.
Here are some captions :
alt text
here are the view of the inspector for my character named "moulon". I precise that i have no other script created in this project, i make my first steps.
Answer by DubstepDragon · Jan 21, 2014 at 04:30 PM
Well, the easier and more mature solution would be to browse the Asset Store for the 2D example project. All you have to do is export it into a UnityPackage and include dependencies, or just import it straight into your project and utilize the scripts attached with the project. They are fully commented and documented in code, and so you can learn from it and modify it as you please.
Your answer
![](https://koobas.hobune.stream/wayback/20220613130954im_/https://answers.unity.com/themes/thub/images/avi.jpg)