- Home /
Why object goes through walls by adding force
I want to throw a grenade (with Rigidbody). Therefore I use AddForce () and it is working. However if I throw the grenade, it sometimes goes through walls. 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
}
}
Comment
Your answer
Follow this Question
Related Questions
Player isn't moving (AddForce) (Hook)(c#) 1 Answer
AddForce mechanism ? 1 Answer
Multiple Cars not working 1 Answer
Lerp to target position in X amount of time 2 Answers
Why object goes sometimes through walls by adding force ? 2 Answers