- Home /
OnColliderEnter2D not working!
Hello again,
i got a problem with the OnColliderEnter2D, because if my character hits the Ground tagged with "Grass" the function OnCollider2D is not called. This is a problem because now my character only jumps 1 time.
Here is my code:
var moveSpeed = 4f;
// Jump of Player
var jumpHeight : float = 500;
var isJumping : boolean = false;
var nextTimetoJump : float;
function Update () {
Movement ();
}
function Movement () {
if(Input.GetKey (KeyCode.D))
{
transform.Translate(Vector2.right * moveSpeed * Time.deltaTime);
}
if (Input.GetKey(KeyCode.A))
{
transform.Translate(-Vector2.right * moveSpeed * Time.deltaTime);
}
if(Input.GetKeyDown(KeyCode.Space))
{
if(isJumping == false){
rigidbody2D.AddForce(Vector2.up * jumpHeight);
Debug.Log("isJuming");
isJumping = true;
}
}
}
function OnCollisionEnter2D (coll: Collision2D)
{
if(coll.collider.tag == "Gass")
{
isJumping = false;
Debug.Log("NoJumping!");
}
}
I am using the latest version of Unity
I hope somebody is able to help me :s
Greetings GreenTee.
Answer by bhartu · Jul 25, 2014 at 12:42 PM
replace the following code :
if(coll.collider.tag == "Gass")
to
if(coll.collider.tag == "Grass")
Answer by daniand17 · Jun 29, 2015 at 10:30 AM
I know this is an old question, but I have a suggestion that has helped me greatly. I suggest creating a script called "Tags" and make some public constants containing the name of your tags. For example:
public class Tags
{
public const string GRASS = "Grass"
public const string PLAYER = "Player";
}
Now you could instead check the tag as:
if(coll.collider.tag == Tags.GRASS) { }
Whenever you add a new tag, also add it to your Tags class, and you'll never have to worry about spelling again. Better to do it this way, as opposed to hard coding those values in each script.
Answer by GreenTee · Jul 25, 2014 at 12:59 PM
Amazing that i am so stupid! I really have to watch at my spelling! Thanks!!