Unity C#:使用自动定位和鼠标外观的相机控制

问题描述 投票:3回答:1

我通常不会在这里发帖,但现在我花了几个小时试图解决这个问题,我已经搜索了网络,但找不到答案。我希望有人能在这里帮助我。我是新手,这是我第一次尝试将两种类型的第三人称相机控制结合在Unity C#中的玩家运动之后:

  1. 当玩家移动时,相机捕捉到预定义的位置和旋转(无鼠标外观)
  2. 按住鼠标按钮时鼠标外观会激活,因此无论玩家的位置是否在变化,相机都会根据鼠标移动进行旋转

它几乎可以工作,除了我似乎无法将鼠标外观重置为其首次预定义的设置。播放器释放鼠标按钮后,#1的代码启动,因此相机似乎返回其默认视图。但是做了进一步的鼠标外观,我注意到相机总是返回到最后一个位置并且旋转它已被停用。即使在玩家激活他的第一个鼠标外观之前,我还需要它回到原来的预定义位置和旋转,这样它对玩家​​来说不会太迷惑。

我尝试了几个代码,但无法让它工作,所以我删除了非工作线,只是发布了我认为适用的那些。请参阅下面的代码。如果有人可以帮助我,我将不胜感激。提前致谢!

编辑:更新代码以使用两种控制摄像头的方法,并添加建议的代码以重置当前的X和Y值。注释/取消注释每个方法调用以进行测试。但我仍然有一个问题,就是无法平滑鼠标外观的缩放。

最终编辑:我再次更新了下面的代码,清理了它,并包含了建议的更改。代码现在应该完全正常运行并且没有紧张情绪。谢谢你的帮助! :-)

最后的最终编辑:鼠标滚轮添加了“视野缩放”,因此完成了代码。

using UnityEngine;
using System.Collections;

public class PlayerViewController : MonoBehaviour {

    // General Vars
    public Transform targetFollow;
    private bool lookAround = false;

    // For SmoothDampFollow
    public Vector3 followDefaultDistance = new Vector3 (0f, 12.0f, -20f);
    public float followDistanceDamp = 0.2f;
    public Vector3 followVelocity = Vector3.one;

    // For Camera Orbit
    public float orbitDistance = 20.0f;
    public float orbitDamp = 5.0f;
    private const float angleMinY = 7.0f;
    private const float angleMaxY = 50.0f;    
    private float currentX = 7.0f;
    private float currentY = 50.0f;

    // For Zooming Field Of View
    public float FOVmin = 50.0f;
    public float FOVmax = 100.0f;
    public float mouseWheelSpeed = 5.0f;

    void Update () {

        if (Input.GetMouseButtonDown (1)) {

            currentX = transform.eulerAngles.y;
            currentY = transform.eulerAngles.x;

        }

        if (Input.GetMouseButton (1)) {

            lookAround = true;

        } else {

            lookAround = false;

        }

        ZoomFOV ();

    }

    void FixedUpdate () {

        if (lookAround) {

            CameraOrbit ();

        } else {

            SmoothDampFollow ();

        }

    }

    void ZoomFOV () {

        if (Input.GetAxis ("Mouse ScrollWheel") > 0) {

            GetComponent<Camera> ().fieldOfView =  GetComponent<Camera> ().fieldOfView - mouseWheelSpeed;

            if (GetComponent<Camera> ().fieldOfView <= FOVmin) { GetComponent<Camera> ().fieldOfView = FOVmin; }

        } else if (Input.GetAxis ("Mouse ScrollWheel") < 0) {

            GetComponent<Camera> ().fieldOfView = GetComponent<Camera> ().fieldOfView + mouseWheelSpeed;

            if (GetComponent<Camera> ().fieldOfView >= FOVmax) { GetComponent<Camera> ().fieldOfView = FOVmax; }

        }

    }

    void SmoothDampFollow () {

        if (!targetFollow) {

            return;

        } else {

            Vector3 wantedPosition = targetFollow.position + (targetFollow.rotation * followDefaultDistance);
            transform.position = Vector3.SmoothDamp (transform.position, wantedPosition, ref followVelocity, followDistanceDamp);
            transform.LookAt (targetFollow, targetFollow.up);

        }

    }

    void CameraOrbit () {

        if (!targetFollow) {

            return;

        } else {

            currentX += Input.GetAxis ("Mouse X");
            currentY += Input.GetAxis ("Mouse Y");
            currentY = Mathf.Clamp (currentY, angleMinY, angleMaxY);
            Vector3 dir = new Vector3 (0, 0, -orbitDistance);
            Quaternion rotation = Quaternion.Euler (currentY, currentX, 0);
            Vector3 wantedPosition = targetFollow.position + rotation * dir;
            transform.position = Vector3.Lerp (transform.position, wantedPosition, Time.deltaTime * orbitDamp);
            transform.LookAt (targetFollow.position);

        }

    }

}
c# unity3d camera controls right-mouse-button
1个回答
0
投票

更新:试试这个

void LateUpdate()
{

    if (lookAround)
    {

        currentX += Input.GetAxisRaw("Mouse X");
        currentY += Input.GetAxisRaw("Mouse Y");
        currentY = Mathf.Clamp(currentY, angleMinY, angleMaxY);
        Vector3 dir = new Vector3(0, 0, -distance);
        Quaternion rotation = Quaternion.Euler(currentY, currentX, 0);

        Vector3 wantedPosition = target.position + rotation * dir;
        transform.position = Vector3.Lerp(transform.position, wantedPosition, Time.deltaTime * damping);

        camTransform.LookAt(target.position);

    }
        ...

void Update()
{
    // Here
    if (Input.GetMouseButtonDown(1))
    {
        currentX = transform.eulerAngles.y;
        currentY = transform.eulerAngles.x;
    }
    if (Input.GetMouseButton(1))
    { 
        ...

我所做的是在Update()中将鼠标按下时将currentX和currentY重置为当前的eulerangle值。我在LateUpdate()中添加了一个位置Lerp到想要的lookAround位置

编辑:

虽然最初按住鼠标右键时仍然没有修复鼠标外观的平滑缩放

试试这个改变

void CameraOrbit()
{
    currentX += Input.GetAxisRaw("Mouse X");
    currentY += Input.GetAxisRaw("Mouse Y");
    currentY = Mathf.Clamp(currentY, angleMinY, angleMaxY);
    Vector3 dir = new Vector3(0, 0, -distance);
    Quaternion rotation = Quaternion.Euler(currentY, currentX, 0);

    // -->
    Vector3 wantedPosition = target.position + rotation * dir;
    transform.position = Vector3.Lerp(transform.position, wantedPosition, Time.deltaTime * damping);
    // <--

    camTransform.LookAt(target.position);
}
© www.soinside.com 2019 - 2024. All rights reserved.