0

我正在尝试制作一个类似于以下 Youtube 视频的 MMO 角色控制器:https ://www.youtube.com/watch?v=fOvf7gRO_aM

基本上,您使用 WASD 来移动。您可以通过鼠标单击和拖动来移动相机,移动时,您的角色现在将沿新的相机方向移动。问题是,我希望当我按下 WASD 时,角色(而不是相机)将面向运动的方向。

我试着用这个:

        if (Input.GetAxis("Vertical") > 0 | Input.GetAxis("Vertical") < 0){
        Quaternion turnAngle = Quaternion.Euler(0, centerPoint.eulerAngles.y, 0);
        character.localRotation = Quaternion.Slerp(character.rotation, turnAngle, Time.deltaTime * rotationSpeed);
    }

角色没有面向正确的方向,所以我尝试了这个

        if (Input.GetAxis("Vertical") > 0 | Input.GetAxis("Vertical") < 0){
        Quaternion turnAngle = Quaternion.Euler(0, centerPoint.eulerAngles.y, 0);
        character.rotation = Quaternion.LookRotation(movement);
    }

但这似乎不起作用。毕竟我是菜鸟:D

这是控制器移动部分的完整代码:

private void Move()
{
    moveFrontBack = Input.GetAxis("Vertical") * moveSpeed;
    moveLeftRight = Input.GetAxis("Horizontal") * moveSpeed;

    Vector3 movement = new Vector3(moveLeftRight, 0, moveFrontBack);
    movement = character.rotation * movement;
    characterController.Move(movement * Time.deltaTime);

    //Animation on move
    if (movement.magnitude != 0)
    {
        anim.SetBool("isWalking", true);
        anim.SetBool("isIdle", false);
    }

    if (movement.magnitude == 0)
    {
        anim.SetBool("isWalking", false);
        anim.SetBool("isIdle", true);
    }

    centerPoint.position = new Vector3(character.position.x, character.position.y + mouseYPosition, character.position.z);

    //The place where things go south it seems
    if (Input.GetAxis("Vertical") > 0 | Input.GetAxis("Vertical") < 0)
    {
        Quaternion turnAngle = Quaternion.Euler(0, centerPoint.eulerAngles.y, 0);
        character.rotation = Quaternion.Slerp(character.rotation, turnAngle, Time.deltaTime * rotationSpeed);
    }

我在控制器上有一个以前的版本,没有用鼠标改变相机,但是正确的角色的行为面向输入的方向:

    private void Move()
{
    moveFrontBack = Input.GetAxis("Vertical") * moveSpeed;
    moveLeftRight = Input.GetAxis("Horizontal") * moveSpeed;

    Vector3 movement = new Vector3(moveLeftRight, 0, moveFrontBack);

    characterController.Move(movement * Time.deltaTime);
    if (movement != Vector3.zero) transform.rotation = Quaternion.LookRotation(movement);
    if (movement.magnitude != 0)
    {
        anim.SetBool("isWalking", true);
        anim.SetBool("isIdle", false);
    }

    if (movement.magnitude == 0)
    {
        anim.SetBool("isWalking", false);
        anim.SetBool("isIdle", true);
    }


    playerCamera.position = new Vector3(character.position.x, character.position.y + yCamera, character.position.z + zCamera);
}

另外,这里是鼠标移动部分:

    void MouseTurnAround()
{
    zoom += Input.GetAxis("Mouse ScrollWheel") * zoomSpeed;

    if (zoom > zoomMin)
        zoom = zoomMin;

    if (zoom < zoomMax)
        zoom = zoomMax;

    playerCamera.transform.localPosition = new Vector3(0, 0, zoom);

    if (Input.GetMouseButton(0))
    {
        mouseX += Input.GetAxis("Mouse X");
        mouseY -= Input.GetAxis("Mouse Y");
    }
    mouseY = Mathf.Clamp(mouseY, -60f, 60f);
    playerCamera.LookAt(centerPoint);
    centerPoint.localRotation = Quaternion.Euler(mouseY, mouseX, 0);
}

我真的没有更多的想法,所以也许聪明的人可以看到他们能做什么!提前致谢..

4

1 回答 1

1

我管理了我的 2 个解决方案的某种混合,并将我的角色作为一个空游戏对象的父对象,该对象接收新的旋转。然后,我让角色通过外观旋转来定向运动。

    private void Move()
{
    moveFrontBack = Input.GetAxis("Vertical") * moveSpeed;
    moveLeftRight = Input.GetAxis("Horizontal") * moveSpeed;

    Vector3 movement = new Vector3(moveLeftRight, 0, moveFrontBack);
    movement = character.rotation * movement;
    characterController.Move(movement * Time.deltaTime);

    //Animation on move
    if (movement.magnitude != 0)
    {
        anim.SetBool("isWalking", true);
        anim.SetBool("isIdle", false);
    }

    if (movement.magnitude == 0)
    {
        anim.SetBool("isWalking", false);
        anim.SetBool("isIdle", true);
    }

    centerPoint.position = new Vector3(character.position.x, character.position.y + mouseYPosition, character.position.z);

    //Rotates the character to move towards new direction
    if (Input.GetAxis("Vertical") > 0 | Input.GetAxis("Vertical") < 0)
    {
        Quaternion turnAngle = Quaternion.Euler(0, centerPoint.eulerAngles.y, 0);
        character.rotation = Quaternion.Slerp(character.rotation, turnAngle, Time.deltaTime * rotationSpeed);
    }
    if (movement != Vector3.zero) lkModel.rotation = Quaternion.LookRotation(movement);
}
于 2018-08-02T20:03:00.363 回答