简体   繁体   English

C#相机在X轴上跟随播放器

[英]C# Camera Follow Player on X-axis

I have a question like this. 我有这样的问题。 I want the camera to follow a player only on the x-axis. 我希望相机仅在x轴上跟随播放器。 The simplest method is to make the MainCamera child for the target. 最简单的方法是使MainCamera成为目标对象。 This is a fairly easy method and makes the movement not with certain eruptions, or trembling, moving quite smoothly. 这是一种相当简单的方法,它使运动不会伴随某些喷发或颤抖而平稳地运动。 The problem is that I would now like to make the MainCamera follow only on the x-axis. 问题是我现在想使MainCamera仅在x轴上跟随。 and I do not know how to do this condition. 而且我不知道该怎么做。 Below you will see the code written by me, but this code does not work when the MainCamera is a child for the target. 在下面,您将看到我编写的代码,但是当MainCamera是目标的子代时,此代码将不起作用。

 /// <summary>
/// Follows to given target on specified axis
/// </summary>
public class FollowerCam : MonoBehaviour
{
    public Transform target;
    public bool followOnY = false;
    public bool followOnX = false;
    public bool constrainedOnX = false;
    public bool constrainedOnY = false;
    public float minY = 0f;
    public float maxY = 0f;
    public float minX = 0f;
    public float maxX = 0f;
    [ReadOnly]
    public Vector3 offset;

    private Vector3 originalPosition;
    private void Start()
    {
        originalPosition = transform.position;
        offset = transform.position - target.position;
    }

    private void Update()
    {
        if ((!followOnX && !followOnY) || target==null)
            return;
        transform.position = target.position + offset;
        Vector3 normalizedPosition = transform.position;
        if (followOnX)
        {
            if (constrainedOnX)
            {
                normalizedPosition.x = Mathf.Clamp(normalizedPosition.x, minX, maxX);
            }
        }
        else
            normalizedPosition.x = originalPosition.x;
        if (followOnY)
        {
            if (constrainedOnY)
            {
                normalizedPosition.y = Mathf.Clamp(normalizedPosition.y, minY, maxY);
            }
        }
        else
            normalizedPosition.y = originalPosition.y;
        normalizedPosition.z = originalPosition.z;
        transform.position = normalizedPosition;

    }
}

I can use this code by attaching it to the camera to follow a target, but the movement is very bad, especially when trying to make a target jump. 我可以通过将其附加到相机上以跟随目标来使用此代码,但是运动非常糟糕,尤其是在尝试使目标跳跃时。 I think this is a problem because the camera goes a little slower after the target. 我认为这是一个问题,因为相机在目标移动后会变慢一点。 Help!!! 救命!!!

In this scenario I would suggest not having the camera as a child and just write a script to allow it to track. 在这种情况下,我建议不要把相机当成小孩,而只写一个脚本来跟踪它。 You can easily allow/disallow the y-movement this way and gives you a lot more freedom for camera movement down the road, should you need it. 您可以通过这种方式轻松地允许/禁止y移动,并在需要时为相机在道路上的移动提供了更大的自由度。

public GameObject player; //assign player gameobject to variable in the inspector
public bool lockY = true;
private Vector3 offset;
private Vector3 tempVect;

void Start()
{
    offset = transform.position - player.transform.position; //store initial camera offset.
}

void LateUpdate()
{
    tempVect = player.transform.position + offset;
    if (lockY) // toggle this to allow or disallow y-axis tracking
        tempVect.y -= player.transform.position.y; // remove y component of player position
    transform.position = tempVect;
}

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

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