Hey, I want to stop a flying ship(Which I move with the keyboard) immediately when it hits a collider. I tried many things like using OnControllerColliderHit or OnCollision enter but nothing works, the ship just goes through the object. Only when my ship got a Rigidbody attached it works but then the ship bounces back and never stops moving(because it is in the air).
Someone got a idea how to do this?
I hope everything is clear, english ist not my first language.
Thanks :).
here I wrote this out for you based on a script I have that is very similar
void OnCollisionEnter(Collision collision)
{
*/you can change this to a float for a more refined knockback technique but you need to change the value to 5.0f or any number of your choosing as long as it has x.x and an f at the end*/
int knockback = 5;
/*if you want a collision with a specific object use this and make sure the object has a tag of ShipKnocker*/
if (collision.gameObject.tag == "ShipKnocker")/*ITS A SHIP KNOCKER AAAA!!! lol*/
{
/*otherwise delete this and the bracket below*/
if (Physics.Raycast(transform.position,Vector3.right,10))
{
rigidbody.velocity = -transform.right * knockback;
}
if (Physics.Raycast(transform.position,Vector3.left,10))
{
rigidbody.velocity = transform.right * knockback;
}
}/*bracket below*/
}
this will knock the ship back a bit depending on how high you have the knockback variable
Do you have a collider on your ship that’s NOT set to trigger? Or is it a character controller?
For collider’s use the OnCollisionEnter
For character controller’s use OnControllerColliderHit
Make sure you put it on the right object. If it still doesn’t work, try debugging it to see if it actually registers to the collisions. Also, if you want collision, do not use transform.Translate; it makes objects pass through colliders.
EDIT: What you should use is up to what you want to do with it. Here’s a list of things you can use: