【问题标题】:Face the player towards a pushable when raycast hits with an angle当光线投射以一定角度击中时,将玩家朝向可推动物体
【发布时间】:2018-10-19 01:07:23
【问题描述】:

我目前正在开发一个推送系统,但遇到了一些问题。玩家使用 CharacterController 可以与场景中的刚体交互。目前,它的工作原理如下:当 Raycast 击中玩家面前的可推动物体并按下按钮时,他将推动该物体,一切正常。当玩家稍微远离可推动并开始推动时,就会出现此问题。 IE。当角色偏离大约 10 度时,但射线仍然命中。我想到的概念是计算光线投射到可推动立方体的角度,并将角色控制器旋转到可推动的角度。问题是我不知道如何实现这一点。我希望有人可以帮助我解决这个概念。如果有人有任何其他概念或想法,请随时分享:)

编辑:我添加了一些脚本

角色控制器部分:

if (inputDir != Vector2.zero)
    {
        float targetRotation = Mathf.Atan2(inputDir.x, inputDir.y) * Mathf.Rad2Deg + cameraT.eulerAngles.y;
        transform.eulerAngles = Vector3.up * Mathf.SmoothDampAngle(transform.eulerAngles.y, targetRotation, ref turnSmoothVelocity, GetModifiedSmoothTime(turnSmoothTime));
    }

    float targetSpeed = ((running) ? runSpeed : movementSpeed) * inputDir.magnitude;
    currentSpeed = Mathf.SmoothDamp(currentSpeed, targetSpeed, ref speedSmoothVelocity, GetModifiedSmoothTime(speedSmoothTime));

    velocityY += Time.deltaTime * gravity;

    Vector3 velocity = transform.forward * currentSpeed + Vector3.up * velocityY;
    controller.Move(velocity * Time.deltaTime);

    currentSpeed = new Vector2(controller.velocity.x, controller.velocity.z).magnitude;

Raycast 设置和推送状态现在关闭:

if (Physics.Raycast(middle, (forward), out hit, distanceForPush))
    {
        if (Input.GetButton("Push") && hit.collider.tag == "PushableLight")
        {
            anim.SetBool("isPushing", true);
            pushForce = playerPushForceLight;
            movementSpeed = pushSpeedLight;
            hit.transform.SendMessage("HitByPlayer", null, SendMessageOptions.DontRequireReceiver);
            if (controller.velocity == Vector3.zero)
            {
                anim.SetFloat("pushSpeedAnim", 0f);
            }
            else
            {
                anim.SetFloat("pushSpeedAnim", 1f);
            }
        }
        else if (Input.GetButton("Push") && hit.collider.tag == "PushableHeavy")
        {
            anim.SetBool("isPushing", true);
            pushForce = playerPushForceHeavy;
            movementSpeed = pushSpeedHeavy;
            hit.transform.SendMessage("HitByPlayer", null, SendMessageOptions.DontRequireReceiver);
            if (controller.velocity == Vector3.zero)
            {
                anim.SetFloat("pushSpeedAnim", 0f);
            }
            else
            {
                anim.SetFloat("pushSpeedAnim", 1f);
            }
        }
        else
        {
            anim.SetBool("isPushing", false);
            pushForce = 0f;
            movementSpeed = walkSpeed;
            hit.transform.SendMessage("HitStopped", null, SendMessageOptions.DontRequireReceiver);

        }
    }
    else
    {
        anim.SetBool("isPushing", false);

    }


    AnimatorStateInfo stateInfo = anim.GetCurrentAnimatorStateInfo(0);
    if (stateInfo.fullPathHash == pushStateHash)
    {
        turnSmoothTime = maxTurnSmoothTimePushing;
    }
    else
    {
        turnSmoothTime = 0.1f;
    }
}

【问题讨论】:

    标签: c# unity3d


    【解决方案1】:

    我会尝试将玩家与命中的碰撞法线对齐。

    transform.rotation = Quaternion.LookRotation(-hit.normal, Vector3.up);
    

    不过,这可能只适用于盒子。

    【讨论】:

    • 这种方法很有效,但我现在遇到了更多问题。我认为这些问题与 pushForce 的发展方向有关。目前pushForce的方向是:Vector3 pushDirection = new Vector3(hit.moveDirection.x, 0, hit.moveDirection.z);这是基于角色控制器如何击中可推动对象,但我认为这应该替换为击中法线所面对的方向。这样,可推对象总是被推向命中法线的方向。问题是,我不知道该怎么做。
    • 我会做 pushDirection = player.transform.forward,但这真的取决于你希望你的游戏玩法如何工作。
    猜你喜欢
    • 1970-01-01
    • 2021-09-07
    • 2021-05-31
    • 2022-06-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多