【问题标题】:Smooth Camera Follow in Unity for 2D PlatformerUnity 中用于 2D 平台游戏的平滑相机跟随
【发布时间】:2021-03-27 04:55:07
【问题描述】:

在过去的几个小时里,我一直在互联网上搜索,试图找到一个可行的解决方案。我已经尝试了所有我能想到的东西:不同类型的函数、不同类型的更新、不同的平滑时间。下面是我的游戏目前如何玩的视频。我正在制作一个小型平台游戏,只是为了练习,我想解决这个相机问题! Click here for video

这是我当前的代码,但同样,我也尝试了许多其他组合。感谢大家的帮助。

using UnityEngine;

public class CameraFollow : MonoBehaviour {

public Transform target;
public Vector3 offset;
public float smoothTime = 0.3f;

private Vector3 velocity;

private void LateUpdate()
{
    transform.position = Vector3.SmoothDamp(transform.position, target.position + offset, ref velocity, smoothTime);
}
}

编辑: 我已经尝试了许多其他建议,但仍然没有任何效果。如果这有帮助,我正在运行 Unity 2018.2.5f1 Personal 64 位。我使用的是 Razer Blade 15 2018。

【问题讨论】:

  • 你试过这个话题的答案了吗? answers.unity.com/questions/29183/2d-camera-smooth-follow.html
  • 是的。不起作用:/。无论如何谢谢:D
  • 你试过使用线性插值吗?
  • 是的。目前,我正在研究游戏玩法。我会在需要时回到相机旁。希望我能以全新的眼光看待它,它会开始工作;P

标签: unity3d game-development


【解决方案1】:

你可以试试下面的代码。这段代码适用于我的 3D 游戏。

public float translationFactor = 20;

void LateUpdate(){
  if(transform.position != target.position) {
    transform.position += (target.position - transform.position) / translationFactor;           
  }
}

这是 Unity3D 的 LateUpdate() documentation 中关于为什么在使用相机时应该使用 LateUpdate() 的直接引述。

在调用所有更新函数之后调用 LateUpdate。这个 对于命令脚本执行很有用。例如跟随相机 应该始终在 LateUpdate 中实现,因为它跟踪对象 可能已经在 Update 中移动了。

我还注意到您在 2D 游戏中使用 Vector3 而不是 Vector2。我在 2D 方面的经验不如我在 3D 方面的经验,所以我不知道用Vector2s 替换Vector3s 是否会有所不同。

【讨论】:

  • 谢谢!我要试试这个。
  • 不幸的是,我仍然在颤抖。我想我要从头开始,看看能把我带到哪里。
【解决方案2】:

我没有对此进行测试,但如果我正确理解您的问题,只需将函数名称从 LateUpdate 更改为 Update,它应该可以工作。

【讨论】:

  • 感谢您的回复,但我仍然感到颤抖。
  • 你能试试FixedUpdate吗?
  • 我也试过了。它变得更好,但随后发生了这种情况。看看这个视频。另一个还没有在 Google Drive 上处理,所以我把这个上传到 YouTube。 youtu.be/YitNRgMVweg 当角色在地上时会发生震动。也许我必须重写我是如何移动角色的?
  • 也许吧。你能展示你的脚本中移动角色的部分吗?
  • 是的。所以我得到了 PlayerController 和 PlayerMovement。 PlayerController 处理输入,而 PlayerMovement 实际移动角色。单击此处获取代码。 docs.google.com/document/d/…
【解决方案3】:

我正在考虑您的骨架脚本的问题。你的相机跟随骨架,你在相机上思考问题 尝试使用AddForce 而不是transform.position 移动骨架 示例

void Update () {
    if (Input.GetMouseButtonDown (0)) {
        GetComponent<AudioSource> ().PlayOneShot (voices[2]);
        birdSpeed.velocity = Vector2.zero;
        birdSpeed.AddForce (new Vector2 (0, birdUp));
    }
public void Buttons(int i){
    if (i == 0) {
        birds = 0.2f;
        GetComponent<Rigidbody2D> ().gravityScale = 4;
        birdUp = 400;
        StartPanel.SetActive (false);
        GamePanel.SetActive (true);
        GameOverPanel.SetActive (false);
    }

在这段代码中,当我点击鼠标时,你可以调整你的代码来适应这个

【讨论】:

  • 感谢您的选择!不幸的是,我仍然有同样的颤抖。我想我要从头开始,看看能把我带到哪里。
【解决方案4】:

我知道我迟到了,但是对于所有提出这个问题的人来说,我调查了你的问题并在我正在开发的游戏中对其进行了测试,我通过将 void LateUpdate() 更改为 void FixedUpdate() 解决了你的问题,这是代码!希望我有所帮助..

using UnityEngine;

    public class CameraFollow : MonoBehaviour {
    
    public Transform target;
    public Vector3 offset;
    public float smoothTime = 0.3f;
    
    private Vector3 velocity;
    
    private void LateUpdate()
    {
        transform.position = Vector3.SmoothDamp(transform.position, target.position + offset, ref velocity, smoothTime);
    }
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-05-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-01
    • 1970-01-01
    相关资源
    最近更新 更多