unity3d 我怎样才能让凸轮忽略y轴,而球员跳跃使用Cinemachine在Unity2D?

j5fpnvbx  于 2023-03-13  发布在  Mac
关注(0)|答案(2)|浏览(151)

大家好,我正在做一个2D平台游戏,我想让摄像头跟随玩家,但是忽略y轴,这样当玩家跳跃时,摄像头会保持在原来的位置,而不是跟随玩家。
示例(参见资产包演示):https://ansimuz.itch.io/gothicvania-church-pack
我如何使用Cinemachine来实现这一点?

vfhzx4xs

vfhzx4xs1#

你不必在相机控制器脚本中覆盖相机的Y值。这是一个非常基本的实现:

using UnityEngine;
using System.Collections;

public class CameraController : MonoBehaviour {

public GameObject player;        //Public variable to store a reference to the player game object
public bool followY = false;

private Vector3 offset;            //Private variable to store the offset distance between the player and camera

// Use this for initialization
    void Start () 
    {
        //Calculate and store the offset value by getting the distance between the player's position and camera's position.
        offset = transform.position - player.transform.position;
    }

// LateUpdate is called after Update each frame
    void LateUpdate () 
    {
        // Set the position of the camera's transform to be the same as the player's, but offset by the calculated offset distance.
        if(followY)
          {
          transform.position = player.transform.position + offset; // we should follow the player Y movements, so we copy the entire position vector
          }
        else
          {
          transform.position = new Vector3(player.transform.position.x, transform.position.y, player.transform.position.z) + offset; // we just copy the X and Z values
          }
    }
}

把这个脚本附加到摄像机上,你可以通过相应地设置布尔值来启用或禁用Y轴移动。如果你永远不需要这个功能,就把这行放在else块中。
希望这对你有帮助!

q8l4jmvw

q8l4jmvw2#

你可以在电影摄影机组件中将死区高度设置为1。然后试试。我觉得很有效。

相关问题