Sorry I know this might be an easy fix but I’ve been at it for hours now. I’m making a 2D platformer and when adding some ground detection my player now detects everything in my tile set.
public class jump : MonoBehaviour
{
[Range(1, 10)]
public float jumpVel;
public bool Grounded;
public BoxCollider2D box;
void Update()
{
if(Input.GetButtonDown("Jump") && Grounded){
GetComponent<Rigidbody2D>().velocity = Vector2.up * jumpVel;
}
}
void OnCollisionStay2D(Collision2D collider)
{
CheckIfGrounded();
}
void OnCollisionExit2D(Collision2D collider)
{
Grounded = false;
}
private void CheckIfGrounded()
{
float extra = 0.1f;
if(Physics2D.Raycast(box.bounds.center, Vector2.down, box.bounds.extents.y + extra)){
Grounded = true;
}
Color rayColor = Color.green;
Debug.DrawRay(box.bounds.center, Vector2.down * (box.bounds.extents.y + extra), rayColor);
}
}
I’m under the impression that raycasting only detects in one direction but i’m extremely new to this. The Grounded bool works normally on the ground and in the air(true on the ground false in the air), however it’s set to true when colliding with walls in the air. Any help would be greatly appreciated.
Thank you so much! This worked right away.
– blocksatjoeThis is the old uJS aka 'UnityScript', Unity javascript, and worked with the Character Controller component back in Unity 3.5 ~ 4. the way variables are declared and typecast is different, e.g. : // uJS var variableName : VariableType; // C# VariableType variableName; There was also another language before Unity5, called Boo
– AlucardJay