C# 使用平滑摄影机的2D角色口吃

C# 使用平滑摄影机的2D角色口吃,c#,unity3d,C#,Unity3d,当我有一个平滑的摄像头跟着我时,我的角色会口吃,尽管这不会让我感到烦恼。这让玩这个游戏的玩家很恼火 那么有没有办法解决这个问题?光滑的相机在我看来真的很好,其他人也一样,但只有口吃需要修复,它会看起来很棒 摄影机脚本: using UnityEngine; using System.Collections; public class CameraController : MonoBehaviour { public GameObject cameraTarget; // object

当我有一个平滑的摄像头跟着我时,我的角色会口吃,尽管这不会让我感到烦恼。这让玩这个游戏的玩家很恼火

那么有没有办法解决这个问题?光滑的相机在我看来真的很好,其他人也一样,但只有口吃需要修复,它会看起来很棒

摄影机脚本:

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

我很确定这是因为在物理时间步长内试图平滑移动目标

我只需要修改你的代码,使其在相机->目标距离的一小部分上平滑一点

e、 g


你能解释一下你的意思吗?可能会在YouTube上发布一段关于这个问题的视频——基本上,这听起来像是你在说你的角色口吃,但你的相机动作很平稳,这表明这个问题与你的相机无关,这使得上面发布的代码在诊断这个问题时毫无用处对不起,应该考虑一下。啊,我明白了,它看起来像乒乓球;从相机代码中的阻尼开始的弹簧式运动。你紧跟着你的角色,平滑会导致相机以不均匀的增量移动。我的问题是,为什么要使用此摄影机代码,而看起来您希望摄影机完全约束到角色?如果将代码更改为
thisTransform.position=cameraTarget.transform.position
,则应该得到完全相同的结果,但不会出现抖动。你是在寻找你的相机跟随中的一个轻微的“死区”吗?如果你考虑到这一点,从逻辑上来说,你是在尝试在0.1f以上平滑移动(我认为这是0.1秒,但unity ref不清楚),但是你的更新是按照物理步骤进行的。这可能意味着角色从摄影机移动10个单位,然后平滑尝试在10个单位上进行平滑,并且仅执行移动的一部分(例如40%,4个单位),然后玩家再移动10个单位,摄影机尝试再移动40%(剩余16个单位,40%=6.4),因此摄影机移动的增量更大,然后用户再次移动10个单位并重复,直到出现抖动!不确定deltaTime是如何插入方程的,所以我可能错了:)哦,谢谢,这很好。尽管出于某种原因,当我走出死区时,它向左移动了1000像素,向下移动了1000像素。你也不能编辑Transform.position.x,所以我必须为它创建一个新的矢量3。我现在正在尝试修复死区bug。好吧,我以前从未使用Unity做过任何事情,所以它主要只是API:P方面的猜测,但很高兴它对您有效!如果你发布更新后的代码,我可能会发现你的bug(如果你幸运的话,哈哈:)。我看不到任何明显的东西,对我来说很好,那么相机多久会跳转这么多?你能再发一段视频吗
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
    }
}