- Home /
Why object goes sometimes through walls by adding force ?
I want to throw a grenade (with Rigidbody and Collider). Therefore I use AddForce () and it is working. However if I throw the grenade, it sometimes goes through walls, even though the walls and the grenade have colliders. Please help me.
This is my code:
using UnityEngine;
using System.Collections;
public class ThrowGrenade :MonoBehaviour
{
public GameObject grenadePrefab;
public Transform grenadeSpawnPoint;
public float throwForce = 20f;
public float delay = 2f;
void Update ()
{
if (Input.GetKeyDown (KeyCode.F))
{
GameObject go = Instantiate (grenadePrefab, grenadeSpawnPoint.position, grenadeSpawnPoint.rotation);
go.GetComponent<Rigidbody>().AddForce (grenadeSpawnPoint.forward * throwForce, ForceMode.Impulse);
Invoke ("Explode", delay);
}
}
void Explode ()
{
//Explode
}
}
And sorry fro my bad English, because it isn't my native language.
Answer by Lukas-Wendt · Apr 28, 2020 at 10:42 AM
Have you tried setting the Collision Detection on the RigidBody on you grenade to something other than Discrete?
Sometimes, fast moving objects are able to move through other colliders. Generally, if you are having issues with fast moving objects and collisions, it is worth trying to set the Collision Detection to something other than Discrete.
Try to look at the documentation for RigidBody under Collision Detection: https://docs.unity3d.com/Manual/class-Rigidbody.html
Putting collision detection to continueus worked for me :)
Answer by ATU15 · Apr 28, 2020 at 10:47 AM
It's working. Thank you very much :D
Great! Can you mark the answer as correct/accept the answer?
Your answer
Follow this Question
Related Questions
Getting a sword in place while attacking an enemy 3 Answers
Stop sword from distorting character animation 3 Answers
Objects with colliders going through walls and each other. 3 Answers
How to check if two objects collide without a collider 1 Answer
Instantiate 1 object after 2 objects collide. ( C# ) 1 Answer