2D角色口吃与平滑的相机

本文关键字:平滑 相机 角色 2D | 更新日期: 2023-09-27 17:49:39

当我有一个平滑的相机跟着我时,我的角色会口吃,尽管这并不让我生气。它惹恼了玩这个的玩家。

那么有什么方法可以解决这个问题吗?流畅的相机看起来真的很好,在我看来,其他人也是如此,但只有口吃需要修复,它会看起来很棒。

拍摄脚本:

using UnityEngine;
using System.Collections;
public class CameraController : MonoBehaviour {
    public GameObject cameraTarget; // object to look at or follow
    public GameObject player; // player object for moving
    public float smoothTime = 0.1f;    // time for dampen
    public bool cameraFollowX = true; // camera follows on horizontal
    public bool cameraFollowY = true; // camera follows on vertical
    public bool cameraFollowHeight = true; // camera follow CameraTarget object height
    public float cameraHeight = 2.5f; // height of camera adjustable
    Vector2 velocity; // speed of camera movement
    private Transform thisTransform; // camera Transform
    // Use this for initialization
    void Start()
    {
        thisTransform = transform;
    }
    // Update is called once per frame
    void Update()
    {
        if (cameraFollowX)
        {
            thisTransform.position = new Vector3(Mathf.SmoothDamp(thisTransform.position.x, cameraTarget.transform.position.x, ref velocity.x, smoothTime), thisTransform.position.y, thisTransform.position.z);
        }
        if(cameraFollowY){
            thisTransform.position = new Vector3(thisTransform.position.x, Mathf.SmoothDamp(thisTransform.position.y, cameraTarget.transform.position.y, ref velocity.y, smoothTime), thisTransform.position.z);
        }
    }
}

2D角色口吃与平滑的相机

我很确定这是由于试图在物理时间步上平滑移动目标

我只是修改你的代码平滑一小部分相机->目标距离与公差

using UnityEngine;
using System.Collections;
public class CameraController : MonoBehaviour {
    public GameObject cameraTarget; // object to look at or follow
    public GameObject player; // player object for moving
    public float smoothTime = 0.1f;    // time for dampen
    public bool cameraFollowX = true; // camera follows on horizontal
    public bool cameraFollowY = true; // camera follows on vertical
    public bool cameraFollowHeight = true; // camera follow CameraTarget object height
    public float cameraHeight = 2.5f; // height of camera adjustable
    Vector2 velocity; // speed of camera movement
    private Transform thisTransform; // camera Transform
    // Charleh - added these tweakable values - change as neccessary
    private float threshold = 0.5f; // Threshold distance before camera follows
    private float fraction = 0.7f;  // Fractional distance to move camera by each frame (smooths out the movement)
    // Use this for initialization
    void Start()
    {
        thisTransform = transform;
    }
    // Update is called once per frame
    void Update()
    {
        // Charleh - updated code here
        if (cameraFollowX)
        {
            if(Math.abs(cameraTarget.transform.position.x - thisTransform.position.x) > threshold)
            {
                // target vector = (target.position - this.position)
                // now multiply that by the fractional factor and your camera will
                // move 70% of the distance (0.7f) towards the target. It will never
                // actually reach the target hence the threshold value (but you probably don't
                // want this as it can result in a noticeable SNAP if you try to put the camera
                // on the target when it's beneath the threshold)
                // Edit: oops, missing brackets which are quite important!
                thisTransform.position.x = (cameraTarget.transform.position.x - thisTransform.position.x) * fraction;
            }
        }
        // Repeat for Y
    }
}