相机跟随播放器 - 问题不流畅
Camera following player - issue being not smooth
我已经为我的相机编写了一些代码,以便它跟随我的角色(我正在制作 3D 横向卷轴 endless-runner/platform 游戏)。
它跟随玩家,但它真的很跳跃,一点也不流畅。我怎样才能解决这个问题?
我避免对角色进行养育,因为我不希望相机在玩家向上跳跃时跟随玩家。
这是我的代码:
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 or 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);
}
}
希望这能帮助您更接近您的解决方案。
我已经为我的相机编写了一些代码,以便它跟随我的角色(我正在制作 3D 横向卷轴 endless-runner/platform 游戏)。
它跟随玩家,但它真的很跳跃,一点也不流畅。我怎样才能解决这个问题?
我避免对角色进行养育,因为我不希望相机在玩家向上跳跃时跟随玩家。
这是我的代码:
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 or 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);
}
}
希望这能帮助您更接近您的解决方案。