- Home /
How to move target when it collides with a bullet
So, I am trying to make a target for my FPS, but I need to make the physics for the rigidbody for the target itself. And I've made a script that should move the rigidbody when the bullet collides with it. But it doesn't work, so should I even have a rigidbody attached to the target game object at all? What am I doing wrong?
Here's my script:
function OnCollisionEnter (Other : Collision){
if(Other.gameObject.layer == "Default"){
GameObject.Destroy(this);
}
if(Other.gameObject.tag == "Target"){
Other.rigidbody.AddForce (Vector3.forward * 10);
}
}
Here's my rigidbody setup:
Answer by robertbu · Oct 24, 2013 at 05:46 PM
You have several issues here. First, if the target is on the 'Default' layer, then you will be destroying this script before it executes the it's add force line. Note that your 'GameObject.Destory(this)' is destroying the script, not the game object. You want 'Destory(gameObject)' to destroy the whole game object.
In adding force, you likely want to use the direction of the bullet and you will need to add far more force than 10 for an object with a mass of 5. Probably like 2500. So the AddForce() would be something like:
Other.rigidbody.AddForce(rigidbody.velocity.normalized * 2500.0);
In addition, there are often problems with fast moving objects and collisions. There are lots of posts on the issue, so a search will turn up a list of partial solutions. Reducing the fixedTimeStep is the most effective place to start.
Your answer
![](https://koobas.hobune.stream/wayback/20220613120553im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Helicopter Crash Collision 0 Answers
Spawn rigidbody on collision 1 Answer
FPS RIGIDBODY PROBLEM 1 Answer
Smart camera? 1 Answer
OnCollisionEnter function problem. JS 2 Answers