- Home /
How should I move an object that parents on collision?
I have a projectile which when fired will parent itself to the object it collides with, rotate according to the normal it hit (detected via raycast), and then activate the relevant decal renderer which is a component of the projectile (not instantiated).
I was using a rigidbody for the projectile and turning it to kinematic when it collides. The problem is of course that because it is colliding with non kinematic rigidbodies and parents itself to them, it transfers some of its force and causes the rigidbodies to always jitter. I think I need to make the projectile a non rigidbody for this to work (if I can still do this with a rigidbody than please let me know). The second problem is though that I was using rigidbody.velocity to move the projectile before, which got fired by a function whenever an input was pressed (not directly in update). If I simply replace this with:
transform.Translate(Vector3(0,0,speed) * Time.deltaTime);
because the function only gets fired while the input is pressed, the projectile doesn't actually translate anywhere. Is there a way I can set an instant velocity to a non rigidbody, which doesn't use update?
Answer by Tomer-Barkan · May 21, 2013 at 07:43 AM
You can set the projectile's collider to trigger by setting the "Is Trigger" checkbox in the inspector (and leave the rigidbody)
This way, the projectile will not use the physics engine collision resolution, and you'll have to manually decide how to resolve the collision (which seems to be what you're doing anyway).
When a trigger collider collides with another collider (trigger or regular), the function OnTriggerEnter()
is invoked on BOTH of the objects. So be sure to implement your code in OnTriggerEnter()
instead of OnCollisionEnter()
. Note that the function receives a Collider
instead of a Collision
.
http://docs.unity3d.com/Documentation/ScriptReference/Collider.OnTriggerEnter.html
Ah, ingenious! I hadn't thought of turning of the collider, but as you said I don't actually use it for collisions (I have the raycast for that). Thanks a lot