- Home /
Problem with raycast shooting
for example. i created 2 cubes "cube1" and "cube2" both with tag "Map" so when i shot (using raycast) "cube1" it will move forward by force that bullet created.. but when i shot "cube2" , the "cube1" will move instead of "cube2"
here's the script:
var range = 500;
var shotForce = 100;
function Update()
{
var bullet : RaycastHit;
if(Physics.Raycast(transform.position, transform.forward, bullet, range))
{
if(bullet.transform.tag == "Map")
{
gameObject.FindWithTag("Map").rigidbody.AddForceAtPosition(transform.forward * shotForce, bullet.point);
}
}
Destroy(gameObject);
}
Comment
Best Answer
Answer by robertbu · Oct 10, 2013 at 12:19 AM
FindWidthTag() is going to find some game object with the tag 'Map', but not necessarily the one your raycast hit. Instead do:
bullet.rigidbody.AddForceAtPosition(transform.forward * shotForce, bullet.point);
This will use the rigidbody of the object the raycast hit.
Your answer
![](https://koobas.hobune.stream/wayback/20220613115639im_/https://answers.unity.com/themes/thub/images/avi.jpg)