我已经为我的相机写了一些代码,这样它就可以跟随我的角色(我正在制作一个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);
}
}
我建议使用Vector3.Slerp或Vector3.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);
}
}
希望这能帮助您更接近您的解决方案。