简体   繁体   English

为什么我不能旋转播放器变换?

[英]Why I can't rotate the Player transform?

The Player is a child of a parent object and the Player have his own child a camera. Player 是父对象的子对象,而 Player 有自己的孩子一个相机。 The Player start when the Z on Rotation is to 50 and X and Y are 0.当旋转中的 Z 为 50 且 X 和 Y 为 0 时,播放器开始。

播放器

Then I'm using the Player Animator controller with animation to change the Z only from 50 to 0. When the game start the Player is changing from 50 to 0 on the Z.然后我使用带有动画的 Player Animator 控制器将 Z 仅从 50 更改为 0。当游戏开始时,播放器在 Z 上从 50 更改为 0。

The Player have some components attached to it I tried to remove each one by one while the game is running but nothing changed/helped.播放器附加了一些组件,我试图在游戏运行时一个一个地删除每个组件,但没有任何改变/帮助。

The Player have attached a Rigidbody and a Controller script.播放器附加了一个刚体和一个控制器脚本。

The Player Camera have attached to it a Player Camera Controller script :播放器相机附加了一个播放器相机控制器脚本:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerCameraController : MonoBehaviour
{
    public float sensitivity = 5.0f;
    public float smoothing = 2.0f;

    private UnityEngine.GameObject player;
    private Vector2 mouseLook;
    private Vector2 smoothV;

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

    // Update is called once per frame
    void Update()
    {
        if (PauseManager.gamePaused == false)
        {
            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;

            mouseLook.y = Mathf.Clamp(mouseLook.y, -90f, 90f);

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

I can use the mouse to rotate the camera around 360 degrees.我可以使用鼠标将相机旋转 360 度。 And it's changing the Player rotation on the Y only when using the mouse.它仅在使用鼠标时更改 Y 上的播放器旋转。

But then I tried while the game is running to set a new values to the Player rotation on the XY and Z but nothing changed.但是后来我尝试在游戏运行时为 XY 和 Z 上的 Player 旋转设置一个新值,但没有任何改变。

For some reason it's changing the rotation and I see the Player rotating with the Animator or with the mouse but when I change the Player rotation values nothing happens.出于某种原因,它正在改变旋转,我看到播放器随着动画器或鼠标旋转,但是当我改变播放器旋转值时什么也没有发生。

I also tried to attach a simple script for testing but nothing changed when pressing on L button :我还尝试附加一个简单的脚本进行测试,但按下 L 按钮时没有任何变化:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Test : MonoBehaviour
{
    private void Update()
    {
        if (Input.GetKeyDown(KeyCode.L))
        {
            var player = GameObject.Find("Player");
            player.transform.Rotate(Vector3.left, 25);
        }
    }
}

I can'r figure out how to rotate the Player and why I can't rotate it on my own but I can with the mouse controller script or the animator ?我无法弄清楚如何旋转播放器以及为什么我不能自己旋转它,但我可以使用鼠标控制器脚本或动画师?

Your PlayerCameraController script is overwriting the rotation based on the field mouseLook .您的PlayerCameraController脚本正在覆盖基于字段mouseLook的旋转。 It doesn't take the current rotation of the player or the camera into account at all, and so it just overwrites any changes.它根本不考虑玩家或相机的当前旋转,因此它只会覆盖任何更改。

Instead of using a mouseLook field, modify the camera's localEulerAngles and use Rotate on the player's transform .不要使用mouseLook字段,而是修改相机的localEulerAngles并在玩家的transform上使用Rotate

public class PlayerCameraController : MonoBehaviour
{
    public float sensitivity = 5.0f;
    public float smoothing = 2.0f;

    private UnityEngine.GameObject player;
    private Vector2 smoothV;

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

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

            md = sensitivity * smoothing * md;
            smoothV.x = Mathf.Lerp(smoothV.x, md.x, 1f / smoothing);
            smoothV.y = Mathf.Lerp(smoothV.y, md.y, 1f / smoothing);

            Vector3 modifiedEulers = transform.localEulerAngles + Vector3.left * smoothV.y; 

            // transform euler angles from [0,360) to [-180,180) before clamp
            modifiedEulers.x = Mathf.Repeat(modifiedEulers.x + 180f, 360f) - 180f;
            modifiedEulers.x = Mathf.Clamp(modifiedEulers.x, -90f, 90f);

            transform.localEulerAngles = modifiedEulers;
            player.transform.Rotate(0f, smoothV.x, 0f);
        }
    }
}

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

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