- Home /
Stop affecting Player's velocity when colliding
I have a Runner
class, which is a Cube with Rigid Body increases speed gradually with codes in Update()
:
rigidbody.AddRelativeForce(new Vector3(speed, 0, 0), ForceMode.Acceleration);
When I collide with a non-rigid body (a primitive capsule), the Runner
stops by the capsule. I have applied the code to destroy the capsule upon collision, but the Runner
's speed is still affected. How can the speed not to be affected by the collision? Did I need to set something on the capsule?
Answer by robertbu · Jan 16, 2014 at 04:03 AM
Set the 'isTrigger' flag to true on the primitive capsule and use OnTriggerEnter() to detect the collision.
After "Is Trigger" of the Capsule is enabled, now the Runner
object ignores the Capsule & pass through it without destroying the capsule. The destroy code is now:
void OnTriggerEnter(Collision collison) {
if (collison.gameObject.tag.Equals("CollectableTypeA")) {
Destroy(collison.gameObject);
}
}
What do I miss this time?
A Trigger has a collider as a parameter, not a collision:
void OnTriggerEnter(Collider collider) {
if (collider.tag.Equals("CollectableTypeA")) {
Destroy(collider.gameObject);
}
}
Answer by HummeL_YT · May 17, 2021 at 09:45 AM
Necro, but I still look for an answer. If I can't use triggers, what I should do? Or... Ok, maybe I can calculate speeds of objects, make with them some calculations, and then just set speeds back, but this isn't a good variant for destructible or\and fast-moving objects. I just wanted to try to make a game with some voxel-destruction, but all my solution variants do not work as I want...
Your answer
Follow this Question
Related Questions
Ignore collision, but raise event. 1 Answer
Edge Collision Issue 1 Answer
OnTriggerEnter2D on an empty game object with a box collider? 1 Answer
No collision detected between two gameObjects 1 Answer
Collision Detection not working 1 Answer