在Unity中,你可以使用以下代码来实现按下按钮时开启相机的跟随功能:

using UnityEngine;

public class CameraFollower : MonoBehaviour
{
    public Transform target;
    public float smoothSpeed = 0.125f;
    public Vector3 offset;

    private bool isFollowing = false;

    private void LateUpdate()
    {
        if (isFollowing)
        {
            Vector3 desiredPosition = target.position + offset;
            Vector3 smoothedPosition = Vector3.Lerp(transform.position, desiredPosition, smoothSpeed);
            transform.position = smoothedPosition;
        }
    }

    public void StartFollowing()
    {
        isFollowing = true;
    }
}

这段代码创建了一个名为CameraFollower的脚本。通过将该脚本附加到相机对象上,并设置相应的targetoffset,可以实现相机跟随目标的效果。

然后,你需要在按钮的响应函数中调用StartFollowing方法来开启相机的跟随功能。例如,你可以在按钮的OnClick事件中调用该方法:

public class ButtonController : MonoBehaviour
{
    public CameraFollower cameraFollower;

    public void OnButtonClick()
    {
        cameraFollower.StartFollowing();
    }
}

在这个例子中,ButtonController是一个控制按钮的脚本。你需要将相机的CameraFollower组件分配给cameraFollower变量,然后在按钮的OnButtonClick方法中调用StartFollowing方法来开启相机的跟随功能。

确保将按钮的OnClick事件绑定到ButtonController脚本的OnButtonClick方法,这样当按钮被点击时,相机的跟随功能就会被启动

unity当按下某个按钮时camera的follower选项则开启的代码

原文地址: http://www.cveoy.top/t/topic/ibYP 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录