- Home /
Shooter FPS problem with Raycast
Hello, I have a problem with position of Camera and weapon shot. If you check the foto. At the left are a red ray that is the real direction os Shot. But my weapon is not at the same position of camera then when i apply the direction to shot, I have this anoying problem and the yellow LineRender (right of picture) touch the cube.
I have cheked games like COD warfare and this problem no appear
This problem is only when I shoot and aren gameobject to hit. Here I leave the Code. (The black quad of screen is a Canvas to simulate th aim ).
void Update()
{
line.SetPosition(0, posGun.position);
Vector3 auxPos_ = cam.position + cam.forward * distance;
if(Physics.Raycast(cam.position, cam.forward, out hit, Mathf.Infinity))
{
line.SetPosition(1, hit.point);
}
else
{
line.SetPosition(1, auxPos_);![alt text][1]
}
Ray ray = Camera.main.ScreenPointToRay(new Vector3(Camera.main.pixelWidth/2f, Camera.main.pixelHeight/2f, 0));
Debug.DrawRay(ray.origin, Camera.main.transform.forward * 10, Color.red);
}
[1]: /storage/temp/167689-shooter-problem.png
Answer by rh_galaxy · Sep 17, 2020 at 06:44 PM
Could this solve it, that you run your raycast from the weapon and not the camera point?
Vector3 auxPos_ = posGun.position + cam.forward * distance;
if(Physics.Raycast(posGun.position, cam.forward, out hit, Mathf.Infinity))
But then, it's not real with the AI$$anonymous$$ picture . This aim picture in image in canvas in center of screen to simulate where you are aim.
Your answer
Follow this Question
Related Questions
Enemy Will not die 1 Answer
Advanced FPS shooting 1 Answer
FPS shooting 1 Answer
FPS recoil 1 Answer
left and right shooting 1 Answer