首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >单位第三人称控制器,旋转噩梦

单位第三人称控制器,旋转噩梦
EN

Stack Overflow用户
提问于 2018-08-01 14:42:27
回答 1查看 2.3K关注 0票数 0

我正在试着做一个像Youtube视频中那样的角色控制器:https://www.youtube.com/watch?v=fOvf7gRO_aM

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

我试着使用这个:

代码语言:javascript
运行
复制
        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);
    }

角色没有朝向正确的方向,所以我试了一下

代码语言:javascript
运行
复制
        if (Input.GetAxis("Vertical") > 0 | Input.GetAxis("Vertical") < 0){
        Quaternion turnAngle = Quaternion.Euler(0, centerPoint.eulerAngles.y, 0);
        character.rotation = Quaternion.LookRotation(movement);
    }

但这似乎并不管用。我毕竟是个菜鸟:D

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

代码语言:javascript
运行
复制
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);
    }

我在控制器上有一个以前的版本,没有用鼠标改变摄像头,但右侧字符的行为面对输入的方向:

代码语言:javascript
运行
复制
    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);
}

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

代码语言:javascript
运行
复制
    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);
}

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

EN

回答 1

Stack Overflow用户

发布于 2018-08-03 04:03:00

我管理了我的两个解决方案的某种混合,并将我的角色设置为一个空游戏对象的父对象,它接收新的旋转。然后,我离开角色的方向,看起来旋转的运动。

代码语言:javascript
运行
复制
    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);
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51626885

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档