【问题标题】:Camera yMin position should be restricted to player position摄像机 yMin 位置应限制在玩家位置
【发布时间】:2016-02-25 14:29:38
【问题描述】:

我正在 Unity 上开发一款 android 游戏,玩家在点击时会跳跃,并且相机会跟随玩家。到目前为止,我所做的是使用yMin = 0yMax = 2 限制/夹住相机的垂直运动。相机也可以流畅地跟随玩家。 我现在想要的是当玩家改变它的位置时,相机位置会改变并将下限yMin更新为玩家的当前位置。这样,如果玩家跌落到视野之外,相机不会跟随他们下降到初始值。 这是我的相机跟随代码。

public class CameraFollow : MonoBehaviour {
    [SerializeField]
    private float yMin;
    [SerializeField]
    private float yMax;
    [SerializeField]
    private float xMin;
    [SerializeField]
    private float xMax;
    private Transform target;

    void Start () {         
        target = GameObject.Find("Player").transform;
    }

    void LateUpdate () {

        Vector3 targetPos = new Vector3 (
            Mathf.Clamp(target.position.x, xMin, xMax),
            Mathf.Clamp(target.position.y, yMin, yMax),
            -10
        );

        transform.position = Vector3.Lerp(transform.position, targetPos, 0.02f);

        if (Input.GetKeyDown(KeyCode.Escape)){
            Application.Quit ();    
        }
    }
}

【问题讨论】:

    标签: unity3d


    【解决方案1】:

    好的,我刚刚发现我应该将相机 y 位置分配给 yMin。

    Update(){
    yMin = GetComponent<Camera>().transform.position.y;
    }
    

    【讨论】:

      猜你喜欢
      • 2021-08-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-09-26
      • 1970-01-01
      • 2021-04-05
      相关资源
      最近更新 更多