相机跟随播放器 - 问题不顺畅

时间:2016-04-07 13:52:41

标签: c# unity3d camera

我已经为我的相机编写了一些代码,以便它跟随我的角色(我正在制作3D横向滚动无尽的跑步/平台游戏)。

它跟随玩家,但它真的很跳跃而且根本不光滑。我怎样才能解决这个问题?

我正在避免让角色养育,因为我不希望相机在向上跳跃时跟随玩家。

这是我的代码:

using UnityEngine;
using System.Collections;

public class FollowPlayerCamera : MonoBehaviour {


    GameObject player;

    // Use this for initialization
    void Start () {

    player = GameObject.FindGameObjectWithTag("Player");

    }

    // Update is called once per frame
    void LateUpdate () {

transform.position = new Vector3(player.transform.position.x, transform.position.y, transform.position.z); 
    }





}

1 个答案:

答案 0 :(得分:1)

我建议使用Vector3.SlerpVector3.Lerp之类的内容,而不是直接指定位置。我包含了一个速度变量,您可以将其调高或调低,以找到相机跟随玩家的完美速度。

using UnityEngine;
using System.Collections;

public class FollowPlayerCamera : MonoBehaviour {

public float smoothSpeed = 2f;
GameObject player;

// Use this for initialization
void Start () {

player = GameObject.FindGameObjectWithTag("Player");

}

// Update is called once per frame
void LateUpdate () {

transform.position = Vector3.Slerp(transform.position, new Vector3(player.transform.position.x, transform.position.y, transform.position.z), smoothSpeed * Time.deltaTime); 
}
}

希望这有助于您更接近解决方案。

相关问题