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)
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