[英]Shooter FPS 3d UNITY
I have a problem with the position of the camera and weapon shot.我的相机和武器射击的位置有问题。 If you check the photo, on the left is a red ray that is the real direction of the shot.
如果你检查照片,左边是一条红色光线,它是拍摄的真实方向。 But my weapon is not at the same position of the camera.
但是我的武器不在相机的同一位置。 When I apply the direction to shot, I have this issue and the yellow
LineRender
(right of the picture) hit the cube.当我将方向应用到拍摄时,我遇到了这个问题,黄色的
LineRender
(图片右侧)击中了立方体。
This problem is only occurs when the shot doesn't hit a gameObject此问题仅在射击未击中游戏对象时发生
The black quad of screen is a Canvas to simulate the crosshair屏幕的黑色四边形是一个 Canvas 来模拟十字准线
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][2]
}
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);
}
Don't simply set the line to where the aiming raycast hits.不要简单地将线设置为瞄准光线投射的位置。 First, do a 2nd raycast from the gun to where the aiming raycast hits.
首先,从枪到瞄准光线投射命中的地方进行第二次光线投射。 Then, if that hits anything, draw the line from the gun to that point instead.
然后,如果它击中任何东西,请改为从枪到该点绘制线。
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))
{
Vector3 fireDirection = hit - posGun.position;
Vector3 hitFromGun;
if (Physics.Raycast(posGun.position, fireDirection, out hitFromGun, Mathf.Infinity))
{
line.SetPosition(1, hitFromGun.point);
}
else
{
line.SetPosition(1, hit.point);
}
}
else
{
line.SetPosition(1, auxPos_);![alt text][2]
}
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);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.