Unity玩家移动问题

huangapple go评论63阅读模式
英文:

Unity player movement issue

问题

I was trying to follow brackeys tutorial on player controller, but the code is not working.

Here's the code:

using UnityEngine;

public class Mouse_Look : MonoBehaviour 
{
    public float mousesen = 100f;
    public Transform playerBody;
    float xRotation = 0f;

    // Start is called before the first frame update
    void Start()
    {
        Cursor.lockState = CursorLockMode.Locked;
        Cursor.visible = false;   
    }

    // Update is called once per frame
    void Update()
    {
        float mouseX = Input.GetAxis("Mouse X") * Time.deltaTime * mousesen;
        float mouseY = Input.GetAxis("Mouse Y") * Time.deltaTime * mousesen;
        
        xRotation -= mouseY;
        xRotation = Mathf.Clamp(xRotation, -90f , 90f );
        
        transform.localRotation=Quaternion.Euler(xRotation,0f,0f);
        playerBody.Rotate(Vector3.up * mouseX);
    }
}

What should I do??

英文:

I was trying to follow brackeys tutorial on player controller, but the code is not working.
Unity玩家移动问题

Here's the code:

using UnityEngine;

public class Mouse_Look : MonoBehaviour 
{
    public float mousesen = 100f;
    public Transform playerBody;
    float xRotation = 0f;

    // Start is called before the first frame update
    void Start()
    {
        Cursor.lockState = CursorLockMode.Locked;
        Cursor.visible = false;   
    }

    // Update is called once per frame
    void Update()
    {
        float mouseX = Input.GetAxis("Mouse X") * Time.deltaTime * mousesen;
        float mouseY = Input.GetAxis("Mouse Y") * Time.deltaTime * mousesen;
        
        xRotation -= mouseY;
        xRotation = Mathf.Clamp(xRotation, -90f , 90f );
        
        transform.localRotation=Quaternion.Euler(xRotation,0f,0f);
        playerBody.Rotate=(Vector3 up * mouseX);
    }
}

What should I do??

答案1

得分: 1

transform.Rotate 是一个方法,而不是一个属性,因此
playerBody.Rotate=(Vector3 up * mouseX);

应该是
playerBody.Rotate(Vector3.up * mouseX, playerBody.rotation.y, playerBody.rotation.y);

英文:

transform.Rotate is a method, not a property, so
playerBody.Rotate=(Vector3 up * mouseX);

should be
playerBody.Rotate(Vector3.up * mouseX, playerBody.rotation.y, playerBody.rotation.y);

答案2

得分: 0

应该是 playerBody.Rotate() 而不是 playerBody.Rotate=()

Unity transform.Rotate 文档链接:

英文:

It should be playerBody.Rotate() instead of playerBody.Rotate=().

Unity transform.Rotate documentation:

答案3

得分: 0

这个部分已翻译:

这个 playerBody.Rotate=(Vector3 up * mouseX);
应该是 playerBody.Rotate(Vector3 up * mouseX);

Rotate 是一个接受一个 Vector3 的方法。我建议花点时间来了解 Unity 中的旋转 基础知识,而不仅仅是从教程中复制粘贴代码。这可能会修复错误,但你将不得不每次都查找教程。

英文:

This playerBody.Rotate=(Vector3 up * mouseX);
Should be playerBody.Rotate(Vector3 up * mouseX);

Rotate is method that takes in a vector 3. I suggest taking some time to understand the basics of rotation in Unity, rather than just copy pasting codes from tutorial. This might fix the error, but you will have to look for tutorial every time.

答案4

得分: 0

playerBody.Rotate=(Vector3 up * mouseX); 转换为 playerBody.Rotate(Vector3.up * mouseX);,因为".Rotate"是一个方法,不需要"="。 Unity玩家移动问题 干杯,伙计!

英文:

playerBody.Rotate=(Vector3 up * mouseX); to playerBody.Rotate(Vector3.up * mouseX); because ".Rotate" is method. you don't need "=". Unity玩家移动问题 cheers mate!

huangapple
  • 本文由 发表于 2023年6月19日 19:31:51
  • 转载请务必保留本文链接:https://go.coder-hub.com/76506207.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定