- Home /
Collider2D/RigidBody2D not working
When searching, I seem to only be able to find people who did not know to use the 2D version of colliders or methods, so it's been difficult to find an answer for this.
I have two objects I want to collide (this game is a top view, so gravity does not come into play here).
When the fireball flies, it goes straight through the player without ever logging a collision. They are both on layer 0. Am I missing something small?
A Fireball: (Note that I have IsTrigger checked, but either way does not seem to make a difference)
And the player (except for it's transform, let me know if that is needed here):
Here is the code on the fireball:
var speed : int = 2;
function Update () {
transform.Translate( Vector3(.5,0,0) * Time.deltaTime * 10);
}
function OnCollisionEnter2D(coll: Collision2D) {
Debug.Log("FIREBALL COLLISION");
if (coll.gameObject.tag == "Player"){
coll.gameObject.SendMessage("ApplyDamage", 34);
}
Destroy(gameObject);
}
And here is the code on the player (though I don't think the problem of 2D collision resides here):
var health = 100;
function ApplyDamage (damage : int) {
health -= damage;
if(health <= 0) {
Die();
}
}
function Die () {
//Die and or Respawn
Destroy(gameObject);
}
Try with removing is$$anonymous$$inematic option from rigidbodies
Answer by HarshadK · Aug 20, 2014 at 07:48 AM
Since your Fireball object has a trigger collider you should use OnTriggerEnter2D instead of OnCollisionEnter2D.
Also from your Fireball and Player game object uncheck Is Kinematic since kinematic ridigbodies don't participate in collisions (Ref: Rigidbody2D.isKinematic).
Now since your Fireball and player is not kinematic anymore you should move it using Rigidbody2D.MovePosition from your FixedUpdate() for proper physics.
Something like:
var speed : Vector2 = Vector2 (2, 0);
function FixedUpdate () {
rigidbody2D.MovePosition(rigidbody2D.position + speed * Time.deltaTime);
}
Perfect, I did what you said and it looks much cleaner and works as intended, except that the fireball does not move in the way it's rotated, so I suppose I will keep using my Vector3 code for now, and try to figure out how to do that properly.
Since they are not kinematic but I do not want gravity to affect them, I turned the gravity scale to 0. Is this the proper way to handle that?
Your answer
Follow this Question
Related Questions
Collision between two kinematic rigidbody triggers 2 Answers
Detect a collision point, but allow pass through collider. 0 Answers
Cirlce Colliders 0 Answers
Can't add Rigidbody component because collision prevents it 2 Answers
Collision returning an error 0 Answers