简体   繁体   English

Unity3D c# 2D 游戏摄像机跟随正在跳跃

[英]Unity3D c# 2D Game Camera Follow is making jumps

I made an 2D endless runner game using Unity, where the Player Object moves upwards, but for the Viewer he remains on the same spot because the Camera is following him.我使用 Unity 制作了一个 2D 无尽的跑步游戏,其中 Player Object 向上移动,但对于 Viewer,他仍然在同一个位置,因为相机在跟随他。 The Following works pretty well, but the problem is that it is making small jumps every few seconds.下面的效果很好,但问题是它每隔几秒钟就会发生一次小跳跃。 The jumps are small and thus are not affecting the gameplay in general but they are big enough to make it seem like the game is not running smoothly.跳跃很小,因此一般不会影响游戏玩法,但它们足够大,看起来游戏运行不顺畅。

I can't even tell whether it is the Camera that is making the jumps or the Player Object.我什至不知道是相机在跳跃还是播放器 Object。 But I think it is the Camera, because the spawning obstacles are also making jumps.但我认为是相机,因为生成的障碍物也在跳跃。 What may be the issue?可能是什么问题? Am I using the right method for the Camera?我是否为相机使用了正确的方法?

public class Player
{
    float speed = 5f;
    void Update()
    {
        transform.Translate(0, speed*Time.deltaTime, 0);
    }
}

And this is the Script which is attached to the Camera:这是附加到相机的脚本:

public class CameraFollow
{
    private Vector3 FollowVector;
    public Transform Player;
    void LadeUpdate()
    {
        FollowVector = Player.position - new Vector3(0, -4f, 10);
        transform.position = Vector3.Lerp(transform.position, FollowVector, Time.deltaTime * 4f);
    }
 }

Does the jump happen every 4 seconds?跳跃是否每 4 秒发生一次? I think the problem might be in your Vector3.Lerp function.我认为问题可能出在您的Vector3.Lerp function 中。 It's basically a loose camera-follow, that takes 4 seconds ( Time.deltaTime * 4f ) to smoothly go from Point A to point B. Every 4 seconds it resets its path, hence the hop.它基本上是一个松散的相机跟随,需要 4 秒( Time.deltaTime * 4f )才能从 A 点平滑 go 到 B 点。每 4 秒它重置它的路径,因此是跳跃。 At least that's my theory.至少这是我的理论。

If you don't mind the camera rigidly following the player, I would just remove the Lerp and just set the transform.position to be the FollowVector value like so:如果您不介意相机严格跟随玩家,我只需删除 Lerp 并将transform.position设置为 FollowVector 值,如下所示:

void LadeUpdate()
    {
        transform.position = Player.position - new Vector3(0, -4f, 10);
    }

Try to use Vector3.MoveTowards尝试使用 Vector3.MoveTowards

float step =  speed * Time.deltaTime;
transform.position = Vector3.MoveTowards(transform.position, Player.position - new Vector3(0, -4f, 10f), step);

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

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