Unity3D c# 2D Game Camera Follow 正在跳跃

Posted

技术标签:

【中文标题】Unity3D c# 2D Game Camera Follow 正在跳跃【英文标题】:Unity3D c# 2D Game Camera Follow is making jumps 【发布时间】:2021-04-08 00:38:36 【问题描述】:

我使用 Unity 制作了一个 2D 无尽的跑步游戏,其中玩家对象向上移动,但对于查看者来说,他保持在同一个位置,因为相机在跟随他。以下效果很好,但问题是它每隔几秒钟就会进行一次小跳跃。跳跃很小,因此一般不会影响游戏玩法,但它们足以让游戏看起来运行不顺畅。

我什至不知道是相机在跳跃还是玩家对象。但我认为是相机,因为生成的障碍物也在跳跃。可能是什么问题?我是否为相机使用了正确的方法?

public class Player

    float speed = 5f;
    void Update()
    
        transform.Translate(0, speed*Time.deltaTime, 0);
    

这是附加到相机的脚本:

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);
    
 

【问题讨论】:

【参考方案1】:

跳跃是否每 4 秒发生一次?我认为问题可能出在您的Vector3.Lerp 函数中。它基本上是一个松散的相机跟随,需要 4 秒 (Time.deltaTime * 4f) 才能顺利地从 A 点移动到 B 点。每 4 秒它会重置它的路径,因此是跳跃。至少这是我的理论。

如果您不介意相机严格跟随玩家,我会移除 Lerp 并将 transform.position 设置为 FollowVector 值,如下所示:

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

【讨论】:

这个解决方案让镜头跟随非常顺畅,但是我需要某种 Lerp,因为玩家有时可以加速 1 秒,这让他走得更高一点,但随后又让他回到之前的相机位置。是否可以在您的解决方案中使用 Lerp 函数? 我现在唯一能想象的就是只有当玩家加速时你才会 Lerp。可能是触发加速时激活 Lerp 1 秒的 IF 语句,否则默认为上述方法。【参考方案2】:

尝试使用 Vector3.MoveTowards

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

【讨论】:

以上是关于Unity3D c# 2D Game Camera Follow 正在跳跃的主要内容,如果未能解决你的问题,请参考以下文章

unity3d Texture2D c#初始化

Unity2D 为啥sprite在scene中显示而game界面中没有?

Unity3d坐标与NGUI坐标转换

我想将图像数据(Texture2D)转换为 Base64 字符串并存储在 Unity3D(C#)中的 JSON 文件中

unity3d中的scene视图中不显示阴影,可是在game视图中正常显示,是怎么回事?

unity3d场景里面放个物体怎么看不见