- Home /
problem with raycasts and tags
i've created a few cubes to test my shot force..i mean when i wanted when i shot at some object that object move forward by force here's the scripts
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); }
So when i shot object "cube1" with tag "Map" it will move forward by force but when i shot object "cube2" also with tag "Map" , the object "cube1" moves instead of object "cube2".
You dont need to findwithtag. You have the object captured in the if statement.
bullet.gameObject.rigidbody.AddForceAtPosition(transform.forward * shotForce, bullet.point);
then i got this error
$$anonymous$$issingFieldException: UnityEngine.RaycastHit.gameObject Boo.Lang.Runtime.DynamicDispatching.PropertyDispatcherFactory.FindExtension(IEnumerable' 1 candidates)
Answer by VesuvianPrime · Oct 09, 2013 at 11:44 PM
You can get the hit collider by looking at the bullet.collider
http://docs.unity3d.com/Documentation/ScriptReference/RaycastHit-collider.html
Your answer
Follow this Question
Related Questions
Multiplayer Chat Script (Problem : mixed names) 1 Answer
RPC isn't called on objects with another name, even though they have the same script. 1 Answer
Problem with Force on rigidbody, not always the same force 0 Answers
script triggerd animations doesnt work help! 0 Answers
Quit Game Button Not Working 3 Answers