- Home /
Dynamically Change Position of RayCast
Hello,
I'm using a Transform to reference where I want to cast a ray from:
var referenceObject : Transform;
if(Physics.Raycast (referenceObject.transform.position, direction, hit))
But I want to be able to also dynamically change the position slightly within the code. For now, just add 3 onto the Z axis:
var firePos = referenceObject.transform.position;
var lineStartPos = Vector3(firePos.x, firePos.y, firePos.z + 3);
The only problem with that code, is that its in world space, and not local to the referenced object.
How would I fix this?
Thanks
The position used in Physics.Raycast() is a world coordinate, not a local one.
@robertbu I think he means the z + 3
, which is relative to the global axis, but should be relative to the local axis... maybe... hard to say
If @Benproductions1 is right that you want to add to the local 'z' (forward) axis, you can do:
var lineStartPos = transform.position + transform.forward * 3.0;
Answer by Benproductions1 · May 27, 2013 at 05:37 AM
Hello,
It's hard to tell what you want to do, but if you want to transform a point from local to world space, you can use referenceObject.TransformPosition(point:Vector3)
to make your point relative to the object.
If you want to change a direction from local to world space, you use TransformDirection
:)
Hope this helps,
Benproductiosn1