- Home /
Start raycast from object
Hey there. I am trying to use ray casting for shooting, but I can't seem to figure out how to get the raycast to start from an object (like a gun) and not the camera. I have not been able to find the answer anywhere and if you could help, I would really appreciate it.
Here is the code I have so far: Physics.Raycast(Camera.main.transform.position, Camera.main.transform.forward)
Thanks!
Answer by frogsbo · Jan 31, 2014 at 08:04 AM
the camera ray cast code in unity reference uses the generic GameObject it is attached to...
this is the way to reference the object a script is attached to: gameObject.transform.position gameObject.transform.forward
you can even not put gameObject. it's optional
so essentially use exactly the same as camera but just reference a different object so figure out object referencing. object is the 1st word of the address of the code, every dot and a line of code separates like an address in unity information going from top objects/components/values into sub components/values. object/components/value
Your answer
Follow this Question
Related Questions
Best way to tell which object(s) are being looked at? 3 Answers
Turret With Raycast Camera 1 Answer
Camera Zoom in&out 2 Answers
Is object at least partly visible? 1 Answer
Object pool shooting not working 0 Answers