- Home /
Can't use Rigidbody.AddForce to RaycastHit.point
I`m making a physical Rigidbody shooting mechanic, to make it more accurate, i used a raycast instead of just Camera.transform.forward. But it doesn't shoot and the bullets just gets destroyed after spawning. The only script that can destroy the bullet is the Bullet script and it only calls it on collision, and there's nothing in the way of the bullet.
Here's the code :
GameObject instProj = Instantiate(projectile, shootPoint.position, shootPoint.rotation);
RaycastHit hit;
if (Physics.Raycast(cam.transform.position, cam.transform.forward, out hit))
{
instProj.GetComponent<Rigidbody>().AddForce(hit.point * instProj.GetComponent<Bullet>().speed * Time.unscaledDeltaTime, ForceMode.Impulse);
}
else
{
instProj.GetComponent<Rigidbody>().AddForce(cam.transform.forward * instProj.GetComponent<Bullet>().speed * Time.unscaledDeltaTime, ForceMode.Impulse);
}
Answer by unity_ek98vnTRplGj8Q · Aug 27, 2020 at 04:33 PM
Try using (hit.point - instProj.transform.position) instead. hit.point is the position that the raycast intercepts an object in world coordinates, but you want to add force in the direction to that point, from the position of the bullet.
Your answer
![](https://koobas.hobune.stream/wayback/20220613010623im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Rigidbody.position causes shaking 1 Answer
RaycastHit behind object 2 Answers
Cancel Input if the direction has a wall in the way 1 Answer
raycast hit add force problem, help needed please 1 Answer
Simple AddForce Scirpt help 1 Answer