Colliding by tag, not working.

Hi All,

I guess i’m doing something stupid with this, but just can not see it.

I’m trying to get a bomb to fire, and if it hits the Playship, to ignore the collision (I don’t want to blow myself up) but if it hits the Ground to destroy itself.

Both the Ground and the Bomb have colliders and rigidbodies set to Is Kinematic and then using this script for the bomb.

var bombSpeedDown : int;
var bombSpeedRight : int;

function Update () {
	
//amount to move bomb
amtToMoveDown = bombSpeedDown * Time.deltaTime;
amtToMoveRight = bombSpeedRight * Time.deltaTime;
	
//move bomb
transform.Translate(Vector3.down * amtToMoveDown);
transform.Translate(Vector3.right * amtToMoveRight);
}

function OnCollisionEnter (collision : Collision) {
	
	//If we hit the ground Destroy
	if (collision.gameObject.tag =="Ground") {
		Destroy(gameObject);
		
	//Ignore PlayerShip when firing
	if (collision.gameObject.tag == "PlayerShip") {
	Physics.IgnoreCollision(collision.collider, collider); 
		}
		
	}

}

The Ground has a tag of Ground and the Playership, PlayerShip.

Hope someone out there can help.

Thanks

Hi,

what does Debug.Log(collision.gameObject.tag) ; say ?


oxl

Hi Oxl,

It’s not saying anything. The bombs are just passing through as if not triggering a collision.

The ground shouldn’t have a rigidbody (those are only for moving objects). Setting the bomb to Is Kinematic and directly setting the position means you’re bypassing the physics system, and therefore collisions can’t work. You need to use physics forces or Rigidbody.MovePosition and turn off Is Kinematic for the bomb.

–Eric

Collisions are not registered if the incoming object is not controlled by the physics system (in your case you are moving the bomb with transform.Translate). You should look at the Rigidbody component to see how to move the bomb with physics.

Thanks Eric and Andeee,

I had wanted to avoid using physics as it really slowed my last game done. (Although there was a lot on screen with that one). However, if you can only do it that way, I guess I should give it ago.

Thanks

Thanks again,

Added the physics and used the Rigidbody.MovePosition, and it all looks (and works) great now.