Mentioned code: Code (CSharp): public class Movement : MonoBehaviour { private Rigidbody2D rb2d; private Collider2D col; public float speed; public float jump; public int LayerMask; private void Start() { rb2d = GetComponent<Rigidbody2D>(); } void FixedUpdate() { float moveHorizontal = Input.GetAxis("Horizontal"); float moveVertical = Input.GetAxis("Vertical"); Vector2 movement = new Vector2(moveHorizontal, moveVertical); rb2d.AddForce(movement * speed); if (col.IsTouchingLayers(LayerMask = Physics2D.AllLayers) && Input.GetKeyDown(KeyCode.Space)) { rb2d.AddForce(Vector2.up * Time.deltaTime * jump); } } } It's my first player controler so please help
Console reports: NullReferenceException: Object reference not set to an instance of an object Movement.FixedUpdate () (at Assets/Movement.cs:24)
I think what you should first do is get the exact type (which type of Collider2d is it?) Then set the variable type to that. If it is a box collider, you'd do: Code (csharp): public BoxCollider2D col; Then, just drag the gameobject into that slot in the inspector. Let me know if that works.
My script looks like that now Code (CSharp): public class Movement : MonoBehaviour { private Rigidbody2D rb2d; public BoxCollider2D col; public float speed; public float jump; public int LayerMask; private void Start() { rb2d = GetComponent<Rigidbody2D>(); col = GetComponent<BoxCollider2D>(); } void FixedUpdate() { float moveHorizontal = Input.GetAxis("Horizontal"); float moveVertical = Input.GetAxis("Vertical"); Vector2 movement = new Vector2(moveHorizontal, moveVertical); rb2d.AddForce(movement * speed); if (col.IsTouchingLayers(LayerMask = Physics2D.AllLayers) && Input.GetKeyDown(KeyCode.Space)) { rb2d.AddForce(Vector2.up * Time.deltaTime * jump); } } } There is no error on console but player dosen't move
Does "speed" variable have a value > 0? Is the mass of your object rather large? Is "Is Kinematic" unchecked (shouldn't be checked) on your rigidbody? Are any constraints on the rigidbody checked (ex. freeze x / y / z)? This script of course is on your player (i.e. dragged on the player)? Friction?