首页 > 解决方案 > 如何使用rigidbody.moveposition移动玩家,但将玩家移动到他正在寻找的位置并根据该移动玩家?

问题描述

到目前为止,这是我的播放器控制器脚本..'''

//Variables
public float speed = 5f;
public float sprintSpeed = 10f;
public float jumpForce = 10f;

private Rigidbody rb;

//Methods
private void Start()
{
    rb = GetComponent<Rigidbody>();
}
private void FixedUpdate()
{
    BasicMovement();
}
void BasicMovement()
{
    //Get Player Input
    float inputX = Input.GetAxis("Horizontal");
    float inputZ = Input.GetAxis("Vertical");

    //Get where is Player moving position
    Vector3 direction = new Vector3(inputX, 0f, inputZ);

    //Move Player
    rb.MovePosition(transform.localPosition + (direction * speed * Time.deltaTime));
}

'''

凸轮控制器脚本:''' //变量 public float mouseSensitivity = 100f; //鼠标灵敏度变量,以便我稍后可以将其更改为我喜欢的 public Transform playerBody; //所以camera知道什么是player body private float xAxisRotation; //沿x轴移动

void Start()
{
    
}

void FixedUpdate()
{
        Cursor.lockState = CursorLockMode.Locked; //Locks mouse cursor at the center of the screen
                                                  //Get Mouse X,Y positions in variables
        float mouseX = Input.GetAxis("Mouse X") * mouseSensitivity * Time.deltaTime; //Gets mouse location at the x-axis
        float mouseY = Input.GetAxis("Mouse Y") * mouseSensitivity * Time.deltaTime; //Gets mouse location at the y-axis

        xAxisRotation -= mouseY; //-ensures the rotation is not flipped
        xAxisRotation = Mathf.Clamp(xAxisRotation, -90f, 90f); //Clamps rotation to -90, 90 so that player cannot look behind

        transform.localRotation = Quaternion.Euler(xAxisRotation, 0f, 0f); //I dont know

        //Implement mouseX position to playerBody so that the entire player moves in the X-Axis
        playerBody.Rotate(Vector3.up * mouseX);
}

'''

如果您需要其他东西请告诉。

标签: c#unity3dgame-development

解决方案


推荐阅读