- Home /
OnCollisionEnter problem (RocketJump script)
I am trying to create a rocketjump script as in TeamFortress2,and it works but the explosion take place even if the rocket hits the Player which I dont want,I want if the rocket hits the ground and the player is in the range the the player receives the knockback.I want this because when I fall from the sky and I shoot below me I still get knockback from the player falling on the rocket,I dont want this collision.What should I do?
private void OnCollisionEnter(Collision collision)
{
explosionOrigin = transform.position;
// Wait one frame before starting, to ensure all objects are affected
Collider[] objectsInRange = Physics.OverlapSphere(explosionOrigin, explosionRadius);
foreach (Collider collision1 in objectsInRange)
{
foreach (Collider hit in objectsInRange)
{
Player player = collision1.GetComponent<Player>();
if (player != null)
{
// Apply knockback force to player if they are in explosion radius
player.Knockback(player.transform.position - explosionOrigin, explosionForce);
}
}
Destroy(rocket);
}
if (collision.gameObject.tag == "groundMask")
{
Destroy(rocket);
}
}
}
Answer by unity_ek98vnTRplGj8Q · Mar 09, 2020 at 02:52 PM
If you don't want your rocket to collide your player I recommend removing collisions between their two object layers. You could put the player on a "Player" layer and the rocket on a "Rocket" layer and then in Edit -> Project Settings -> Physics you can turn off the collisions between those layers in the collision matrix
Your answer
Follow this Question
Related Questions
Picked up objects passes through objects 2 Answers
Help with destroying objects on collision 2 Answers
Object change direction after collide with controller 2 Answers
Ignore explosion is there a way? 0 Answers