简体   繁体   English

当玩家在 Unity2D 中使用 Cinemachine 跳跃时,如何让凸轮忽略 y 轴?

[英]How can I make the cam ignore the y axis while player jumps using Cinemachine in Unity2D?

Good day, I'm making a 2D platformer game and I'm trying to make to cam follow the player.美好的一天,我正在制作一个 2D 平台游戏,我正在尝试让 cam 跟随玩家。 But ignore the y-axis so when the player jumps the cam stays in positions instead of following the player.但是忽略 y 轴,因此当玩家跳跃时,凸轮会停留在原地而不是跟随玩家。

Example (see asset pack demo): https://ansimuz.itch.io/gothicvania-church-pack示例(参见资产包演示): https : //ansimuz.itch.io/gothivania-church-pack

How can I do this using Cinemachine?如何使用 Cinemachine 执行此操作?

You don't have to override the camera's Y value in your camera controller script.您不必在相机控制器脚本中覆盖相机的 Y 值。 This would be a very basic implementation:这将是一个非常基本的实现:

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
          }
    }
}

Attach this script to the camera and you can enable or disable the Y-axis movement by setting the boolean accordingly.将此脚本附加到相机,您可以通过相应地设置布尔值来启用或禁用 Y 轴移动。 In case you will never need this functionality, just keep the line in the else block.如果您永远不需要此功能,只需将该行保留在 else 块中。

I hope this will help you!我希望这能帮到您!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM