【问题标题】:Camera following player - issue being not smooth相机跟随播放器 - 问题不流畅
【发布时间】:2016-04-07 13:52:40
【问题描述】:

我为我的相机编写了一些代码,以便它跟随我的角色(我正在制作一个 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); 
    }





}

【问题讨论】:

  • 那么你需要制定一个算法来绑定你的相机,但要让它平滑变化,以便它赶上,或者更优雅地向上/向下移动,而不是像它有一个适合,例如 if camera.y > person.y then if diff>100 camera.y -10 else camera.y -1 (例如,如果出现大跌幅,它会移动更多,但会赶上每一帧它重绘..但是,没有剧烈移动

标签: c# unity3d camera


【解决方案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); 
}
}

希望这可以帮助您更接近您的解决方案。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-01-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-08-23
    相关资源
    最近更新 更多