【问题标题】:Unity 3D movement and camera controllerUnity 3D 运动和相机控制器
【发布时间】:2018-08-07 03:57:35
【问题描述】:

现在我有一个附有动作脚本的第一人称角色。我使用刚体和角色控制器。相机控制父对象与玩家对象的转换,在本例中为胶囊。

玩家对象上面有移动脚本,相机上面有一个相机控制器,只允许鼠标控制玩家对象的变换。

当我跳跃时,我的脚本这样做是为了让玩家保持动量,并且不能在半空中停下来,但是如果我转动我的相机,玩家对象也会根据变换改变轨迹。如果我向右看,在半空中,我的跳跃也会跟随摄像机向右。我希望在半空中跳跃运动,保持原来的轨迹。有人可以帮我添加该功能,但相机仍然可以环顾四周吗?

我的动作脚本:

void movement () {
        moveVector = Vector3.zero;
        moveVector.x = Input.GetAxisRaw("Horizontal");
        moveVector.z = Input.GetAxisRaw("Vertical");

        if (controller.isGrounded) {
            verticalVelocity = -1;

            if (Input.GetButtonDown("Jump")) {
                verticalVelocity = jumpforce;
            }

        } else {
            verticalVelocity -= gravity * Time.deltaTime;
            moveVector = lastMove;
        }

        moveVector.y = 0;
        moveVector.Normalize ();
        moveVector *= playerspeed;
        moveVector.y = verticalVelocity;

        worldMove = transform.TransformDirection (moveVector);
        controller.Move (worldMove * Time.deltaTime);

        //controller.Move (moveVector.z * transform.forward * Time.deltaTime);
        //controller.Move (moveVector.x * transform.right * Time.deltaTime);
        //controller.Move (moveVector.y * transform.up * Time.deltaTime);

        //controller.Move (moveVector * Time.deltaTime);
        lastMove = moveVector;
    }

它是:

moveVector = lastMove;

这样做是为了让我的播放器不会停在半空中。

此外,如果可以做到这一点,那就太棒了,因为你不能在空中改变方向,但你可以稍微拖动轨迹,但要以一种平稳的方式,就像在反恐精英:全球攻势等中一样。

更新: 这是我的相机代码:

public class CameraController : MonoBehaviour {

    Vector2 mouseLook;
    Vector2 smoothV;
    public float sensitivity = 5.0f;
    public float smoothing = 2.0f;

    GameObject character;

    // Use this for initialization
    void Start () {
        character = this.transform.parent.gameObject;
    }

    // Update is called once per frame
    void Update () {
        var md = new Vector2 (Input.GetAxisRaw ("Mouse X"), Input.GetAxisRaw ("Mouse Y"));

        md = Vector2.Scale (md, new Vector2 (sensitivity * smoothing, sensitivity * smoothing));
        smoothV.x = Mathf.Lerp (smoothV.x, md.x, 1f / smoothing);
        smoothV.y = Mathf.Lerp (smoothV.y, md.y, 1f / smoothing);
        mouseLook += smoothV;

        //Låser kameraret så man ikke kan kigge længere ned eller op end 90 grader
        mouseLook.y = Mathf.Clamp (mouseLook.y, -90f, 90f);

        transform.localRotation = Quaternion.AngleAxis (-mouseLook.y, Vector3.right);
        character.transform.localRotation = Quaternion.AngleAxis (mouseLook.x, character.transform.up);
    }
}

【问题讨论】:

    标签: c# unity3d animation scripting


    【解决方案1】:

    这是我用于我的项目的spectate camera controller的部分代码,希望对你有所帮助。

    void Update()
    {
        CameraMovementValidation();
        CameraRotationValidation();
        Camerazoom();
    
        if (Input.GetKey(KeyCode.LeftShift))
        {
            doSlowMotion = true;
        }
        else if (Input.GetKeyUp(KeyCode.LeftShift))
        {
            doSlowMotion = false;
        }
    }
    
    public void FixedUpdate()
    {
        CameraMovement();
        CameraRotation();
    }
    
    public void LateUpdate()
    {
        Controller();
    }
    
    private void CameraMovementValidation()
    {
        if (Input.GetKey(KeyCode.W))
        {
            doCameraUpDownMove = true;
            cameraUpDownMoveDirection = 1;
        }
        else if (Input.GetKey(KeyCode.S))
        {
            doCameraUpDownMove = true;
            cameraUpDownMoveDirection = -1;
        }
        else
        {
            doCameraUpDownMove = false;
        }
    
        if (Input.GetKey(KeyCode.A))
        {
            doCameraLeftRightMove = true;
            cameraLeftRightDirection = -1;
        }
        else if (Input.GetKey(KeyCode.D))
        {
            doCameraLeftRightMove = true;
            cameraLeftRightDirection = 1;
        }
        else
        {
            doCameraLeftRightMove = false;
        }
    
        if (Input.GetKey(KeyCode.Q))
        {
            doCameraForwardBackMove = true;
            cameraForwardBackDirection = 1;
        }
        else if (Input.GetKey(KeyCode.E))
        {
            doCameraForwardBackMove = true;
            cameraForwardBackDirection = -1;
        }
        else
        {
            doCameraForwardBackMove = false;
        }
    }
    
    private void CameraMovement()
    {
        if (doCameraUpDownMove)
        {
            cameraPosition += transform.rotation * Vector3.up * moveCoefficent * cameraUpDownMoveDirection * camereSlowMotionMoveCoefficient;
        }
    
        if (doCameraLeftRightMove)
        {
            cameraPosition += transform.rotation * Vector3.right * moveCoefficent * cameraLeftRightDirection * camereSlowMotionMoveCoefficient;
        }
    
        if (doCameraForwardBackMove)
        {
            cameraPosition += transform.rotation * Vector3.forward * moveCoefficent * cameraForwardBackDirection * camereSlowMotionMoveCoefficient;
        }
    }
    
    private void CameraRotationValidation()
    {
        if (Input.GetMouseButtonDown(1))
        {
            mouseXUpdateForRotation = Input.mousePosition.x;
            mouseYUpdateForRotation = Input.mousePosition.y;
        }
    
        if (Input.GetMouseButton(1))
        {
            doCameraRotation = true;
        }
        else
        {
            doCameraRotation = false;
        }
    }
    
    private void CameraRotation()
    {
        if (doCameraRotation)
        {
            if (Input.mousePosition.x != mouseXUpdateForRotation)
            {
                cameraRotationX += (Input.mousePosition.x - mouseXUpdateForRotation) * rotationFactor;
                mouseXUpdateForRotation = Input.mousePosition.x;
            }
    
            if (Input.mousePosition.y != mouseYUpdateForRotation)
            {
                cameraRotationY += (Input.mousePosition.y - mouseYUpdateForRotation) * rotationFactor;
                mouseYUpdateForRotation = Input.mousePosition.y;
            }
        }
    }
    
    private void Camerazoom()
    {
        mouseScrollCoefficient = Input.GetAxis("Mouse ScrollWheel");
        if (mouseScrollCoefficient > 0)
        {
            zoom += mouseScrollCoefficient * -cameraZoomCoefficent;
        }
        else if (mouseScrollCoefficient < 0)
        {
            zoom += mouseScrollCoefficient * -cameraZoomCoefficent;
        }
    }
    
    private void Controller()
    {
        transform.position = Vector3.Lerp(transform.position, cameraPosition, moveSmoothlyCoefficient);
        transform.rotation = Quaternion.Lerp(transform.rotation, Quaternion.Euler(-cameraRotationY, cameraRotationX, 0), rotationSmoothlyCoefficient);
        camera.fieldOfView = Mathf.Lerp(camera.fieldOfView, zoom, zoomSmoothlyCoefficient);
    
        if (doFolowToPlayer && transform.position.x <= cameraPosition.x + 0.5 && transform.position.x > cameraPosition.x - 0.5 &&
            transform.position.y <= cameraPosition.y + 0.5 && transform.position.y > cameraPosition.y - 0.5 &&
            transform.position.z <= cameraPosition.z + 0.5 && transform.position.z > cameraPosition.z - 0.5)
        {
            cameraRotationY = -mainCamera.transform.localEulerAngles.x;
            cameraRotationX = mainCamera.transform.localEulerAngles.y;
            doRotationFolowToPlayer = true;
            doFolowToPlayer = false;
        }
    
        if (doRotationFolowToPlayer && transform.localEulerAngles.x <= -cameraRotationY + 0.5 && transform.localEulerAngles.x > -cameraRotationY - 0.5 &&
            transform.localEulerAngles.y <= cameraRotationX + 0.5 && transform.localEulerAngles.y > cameraRotationX - 0.5)
        {
            doRotationFolowToPlayer = false;
        }
    }   
    

    【讨论】:

      【解决方案2】:

      您似乎将运动与相机的方向混淆了。在第一人称中,“玩家”通常会在与摄像机相同的方向上移动位置,但不一定非要如此。相机方向由鼠标控制,但您使用相机方向来设置行进方向。

      您需要单独存储当前的方向向量,并根据一些算法(例如双曲线)改变方向向量,并在玩家跳跃期间忽略相机方向。您可以使用相机方向来更改视图矩阵,但不要在飞行时使用相机方向更改相机位置。

      【讨论】:

      • 您能告诉我如何处理我的代码吗?我更新了,所以我的相机代码也在帖子中。但考虑到摄像机的视线,我仍然希望 W 继续前进。我还不是很擅长编码(几天前开始)
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-03-05
      • 2022-07-17
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多