- Home /
Ray origin moving with player movement and mouse movement
I'm using a ray cast for a gun's laser sight with a line renderer for the visuals and I have this problem where the ray's origin changes depending on the player's movement and mouse movement. I had the same problem earlier in my project, but sometimes unity seems to act strange for seemingly no reason which fixes with just a restart of unity. Anyway, this time I can't seem to get it to return to how it was. Here is the code for updating the ray.
private void Update () {
Ray laserRay = new Ray(gunBarrel.position, gunBarrel.forward);
RaycastHit hitInfo;
if (laser == null) {
createLine();
}
laser.SetPosition(0, laserRay.origin + new Vector3(0, laserOffset, 0));
if (Physics.Raycast(laserRay, out hitInfo, laserDistance) && !hitInfo.collider.gameObject.CompareTag("Projectiles")) {
laser.SetPosition(1, hitInfo.point);
} else {
laser.SetPosition(1, laserRay.origin + laserRay.direction * laserDistance);
}
}
I just can't figure out what's causing this because there really isn't that much to the code. Also I'm pretty new to this so sorry if I missed something obvious.
Answer by lgarczyn · Dec 09, 2019 at 02:20 AM
As @Fariborzzn pointed out, you are not using the same origin for the raycast and for the LineRenderer
Apart from that, using LateUpdate or the script order window could help you, as your character controller or animator might update your position after your code has set the positions/rotation of the camera or player.
Late update seemed to fix it for me. The line does flicker every now and then but it's a much better problem than what I had, thanks. Also yeah, the ray is just co$$anonymous$$g from the barrel so I could use it for both the laser sight and the actual bullets, but I ended up just going with hitscan as I couldn't seem to get the bullets to work properly. Anyway, thank you again.
Answer by Fariborzzn · Dec 08, 2019 at 05:08 PM
Hey Genzuo i look in to your code.. you are casting a ray every frame in your update function
Ray laserRay = new Ray(gunBarrel.position, gunBarrel.forward);
if your orgin is not static its because of gunBarrel.position is dynamic (gunbarrel position is changing over time and it should be! because player need to walk and shoot in diffrent direction base on input ) if your problem is something else it would be great if you post a picture of it i will be happy if i can help
Sorry I thought this would have happened, it's a little hard to explain the problem. Basically if I moved left the laser origin would move off to the right, 'detaching' from the gun. Then when I moved my mouse the laser origin would lag behind where it was meant to be. As @ceandros suggested though, late update fixed it.
Your answer
Follow this Question
Related Questions
Using Ray cast for click to move 2 Answers
script problem even though i have no errors 2 Answers
scripting problem 0 Answers
Can you figure out raycast origin position from RacyastHit? 2 Answers
I need help with dragging objects 1 Answer