Reputation: 91
I have been coding a game for about an hour now and I have run into an error with my script that I can't seem to fix. What I mainly seem to be focusing on is the isGrounded update part of my script:
private void OnCollisionExit2D(Collision2D collision)
{
isGrounded = false;
}
private void OnCollisionEnter2D(Collision2D collision2)
{
isGrounded = true;
}
But I am concerned it might be a different part of my script because I don't think anything is wrong with this part. Here is the full script:
using UnityEngine;
using System.Collections;
public class move : MonoBehaviour
{
public float speed;
float moveVelocity;
public float jumpPower = 300.0f;
private bool isGrounded = false;
private bool facingEast = false;
void Update()
{
moveVelocity = 0;
//Left Right Movement
if (Input.GetKey(KeyCode.LeftArrow) || Input.GetKey(KeyCode.A))
{
moveVelocity = -speed;
facingEast = false;
}
if (Input.GetKey(KeyCode.RightArrow) || Input.GetKey(KeyCode.D))
{
moveVelocity = speed;
facingEast = true;
}
GetComponent<Rigidbody2D>().velocity = new Vector2(moveVelocity, GetComponent<Rigidbody2D>().velocity.y);
Rigidbody2D rb = GetComponent<Rigidbody2D>();
if (Input.GetKeyDown(KeyCode.UpArrow) || Input.GetKeyDown(KeyCode.Space))
{
if (isGrounded == true)
{
rb.AddForce(Vector2.up * jumpPower);
}
}
}
private void OnCollisionExit2D(Collision2D collision)
{
isGrounded = false;
}
private void OnCollisionEnter2D(Collision2D collision2)
{
isGrounded = true;
}
}
Sorry if this is a bad question or if there is an obvious answer and I am just being stupid. I am still fairly new to C#
Upvotes: 1
Views: 72
Reputation: 90630
In general in OnCollisionEnter2D
and OnCollisionExit2D
you should check what you are colliding with!
e.g. using Tags and CompareTag
like
private void OnCollisionExit2D(Collision2D collision)
{
if(!collision.transform.CompareTag("Ground")) return;
// only do this if the thing you don't collide
// anymore is actually the ground
isGrounded = false;
}
private void OnCollisionEnter2D(Collision2D collision2)
{
if(!collision.transform.CompareTag("Ground")) return;
// only do this if the thing you collided with is actually the ground
isGrounded = true;
}
otherwise what happens when you collide with a wall is a OnCollisionEnter2D
followed by a OnCollisionExit2D
=> isGrounded = false;
Also in general: Don't use GetComponent
in Update
and especially not for getting the same reference in 3 different places!
Rather get it once in Awake
or already assign it via the Inspector
// already assign via the Inspector by drag&drop
[SerializeField] private Rigidbody2D _rigidbody;
// as fallback assign ONCE in the beginning
private void Awake()
{
if(!_rigidbody) _rigidbody = GetComponent<Rigidbody2D>();
}
and later reuse it
void Update()
{
moveVelocity = 0;
//Left Right Movement
if (Input.GetKey(KeyCode.LeftArrow) || Input.GetKey(KeyCode.A))
{
// I would use -= and += here so when pressing both buttons
// you simply stay at 0. But that's only my opinion of course
// Alternatively I would use 'else if' in order to make the blocks exclusive
moveVelocity -= speed;
facingEast = false;
}
if (Input.GetKey(KeyCode.RightArrow) || Input.GetKey(KeyCode.D))
{
moveVelocity += speed;
facingEast = true;
}
_rigidbody.velocity = new Vector2(moveVelocity, _rigidbody.velocity.y);
// here it is cheeper to first check the isGrounded flag
// and only if it is true get the input
if (isGrounded && (Input.GetKeyDown(KeyCode.UpArrow) || Input.GetKeyDown(KeyCode.Space)))
{
_rigidbody.AddForce(Vector2.up * jumpPower);
}
}
Upvotes: 1