简体   繁体   English

将相机向前移动到游戏对象 position

[英]Move camera to a gameobjects forward position

This is going to be hard to explain but I'll give it a shot.这将很难解释,但我会试一试。 I have a camera set up like this...我有一个这样的相机设置... 在此处输入图像描述

The code for the camera is this... transform.position = new Vector3(car.transform.position.x-posX+carPos, car.transform.position.y+posY, car.transform.position.z-posZ) + car.transform.forward * (Mathf.Clamp(speed, 0, 0.70f)*40); The code for the camera is this... transform.position = new Vector3(car.transform.position.x-posX+carPos, car.transform.position.y+posY, car.transform.position.z-posZ) + car.transform.forward * (Mathf.Clamp(speed, 0, 0.70f)*40);

Which basically equates to get the car x, y and z, displace it a bit above back and left.这基本上等同于让汽车 x、y 和 z 将它移到后面和左边一点。 The + car.transform.forward * (Mathf.Clamp(speed, 0, 0.70f)*40); + car.transform.forward * (Mathf.Clamp(speed, 0, 0.70f)*40); is saying look ahead of the car by a small amount depending on the speed of the car so when we are stationary the car is in the center of the camera, when the car is travelling fast point the camera slightly ahead of the car.是说根据汽车的速度向前看一点点,所以当我们静止时,汽车位于相机的中心,当汽车快速行驶时,相机稍微向前一点。 The problem I have is that the last part makes the camera snap around when the car is turning quickly.我遇到的问题是,当汽车快速转弯时,最后一部分会使相机快速转动。 I am hoping I can smooth out the position of the camera to kind of chase the position it needs to track.我希望我可以平滑相机的 position 以追逐它需要跟踪的 position。 If you can imagine the existing position as a dot in front of the car, I want the camera to chase that dot smoothly instead of being really snappy.如果您可以将现有的 position 想象成车前的一个点,我希望相机能够平稳地追逐那个点,而不是非常活泼。

Example例子

So not sure how to go about this.所以不知道如何 go 关于这个。 Will I need to get the current position and Lerp to the next frame position?我需要获取当前的 position 和 Lerp 到下一帧 position 吗? Lerping between one frame a fraction of distance doesn't seem right to me.在我看来,在一帧的一小部分距离之间徘徊并不合适。 It's just too snappy and I'd rather it be smooth.它太活泼了,我宁愿它是平滑的。

Thanks谢谢

I would use Vector3.SmoothDamp for this.我会为此使用Vector3.SmoothDamp To make it work, you'll need to keep track of the camera's (relative) velocity and the camera's offset due to speed between frames.为了使其工作,您需要跟踪相机的(相对)速度以及由于帧之间的速度而导致的相机偏移。

Altogether, with some parameters you can fine tune to be appropriate this might look like this,:总而言之,您可以使用一些参数进行微调以使其适合,这可能如下所示:

Vector3 camVelocity = Vector3.zero;
Vector3 camSpeedOffset = Vector3.zero;
float cameraSmoothTime = 0.1f;
float maxCameraOffsetVelocity = 1f;

...

Vector3 camSpeedOffsetTarget = car.transform.forward * (Mathf.Clamp(speed, 0, 0.70f)*40);
camSpeedOffset = Vector3.SmoothDamp(camSpeedOffset, camSpeedOffsetTarget, 
        ref camVelocity, cameraSmoothTime, maxCameraOffsetVelocity);

transform.position = new Vector3(
        car.transform.position.x-posX+carPos, 
        car.transform.position.y+posY, 
        car.transform.position.z-posZ) 
      + camSpeedOffset;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM