- Home /
Question by
Noxury · May 17, 2015 at 08:23 PM ·
rotationraycastlocalposition
Raycast on LocalCoordinates
Hello,
I have gates with different pos-, rot-values that should play the open-animation when the player is in range of their ray.
var isOpen : boolean;
var rayLength : int = 3;
private var hit : RaycastHit;
function Update(){
var startpos : Vector3 = transform.TransformPoint(0,0,1.75); //middle of gate's z-length
var dir : Vector3 = Vector3(rayLength,0,0);
if(Physics.Raycast(startpos, dir.normalized, hit, rayLength)){
Debug.DrawRay(startpos, dir, Color.green);
GateOpen();
} else {
Debug.DrawRay(startpos, dir, Color.red);
GateClose();
}
}
Now the direction of the rays are in world coordinates always towards positive x (compare wrong versions in picture), caused by the dir-Vector which handles in world coordinates. How can I make the rays point always in their local x direction?
rayissue.png
(997 B)
Comment
Best Answer
Answer by Noxury · May 20, 2015 at 04:27 PM
Never mind. I've found an other command: LineCast. It needs the start and endpos which can be treated in local coordinated by using TransformPoint. The code would look like this:
var isOpen : boolean;
var rayLength : int = 3;
private var hit : RaycastHit;
function Update(){
var startpos : Vector3 = transform.TransformPoint(0,0,1.75);
var endpos : Vector3 = transform.TransformPoint(rayLength,0,1.75);
if(Physics.Linecast(startpos, endpos, hit)){
Debug.DrawLine(startpos, endpos, Color.green);
BarrierOpen();
} else {
Debug.DrawLine(startpos, endpos, Color.red);
BarrierClose();
}
}
Hope it helps someone got the same problem.
Your answer
![](https://koobas.hobune.stream/wayback/20220613195832im_/https://answers.unity.com/themes/thub/images/avi.jpg)