当光线投射成一定角度时,将播放器面向播放器

时间:2018-10-18 16:47:43

标签: c# unity3d

我目前正在开发推送系统,但遇到了一些问题。播放器使用可以与场景中的刚体进行交互的CharacterController。当前,此方法的工作方式如下:当Raycast击中播放器前面的可推动对象并按下按钮时,他将推动该对象,一切正常。当播放器稍微偏离可推动物并开始推动时,就会发生此问题。即当角色偏离大约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;
    }
}

1 个答案:

答案 0 :(得分:1)

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

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

这可能仅适用于盒子。