您好,我目前正在使用第三人控制器,并且在玩家发生碰撞时遇到跳跃问题。目前,当玩家与立方体碰撞但用户试图向立方体方向移动时,当玩家不再碰撞时(Midair),玩家将朝着用户最初试图前进的方向前进。我想要它,以便当用户在碰撞时跳跃,即使玩家不再在较高点碰撞,玩家也只会直线上升。我怎么做到这一点?我尝试过不同的东西,比如使用光线投射和碰撞器,但遗憾的是没有运气。
我现在拥有的相关脚本:
void Move(Vector2 inputDir, bool running)
{
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 : walkSpeed) * 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;
// Checks if player is not grounded and is falling
if (GroundCheck())
{
velocityY = 0;
anim.SetBool("onAir", false);
}
else
{
anim.SetBool("onAir", true);
}
}
void JumpWalk()
{
if (JumpCheck())
{
float jumpVelocity = Mathf.Sqrt(-2 * gravity * jumpHeightIdle);
velocityY = jumpVelocity;
anim.SetTrigger("JumpWalk");
canJump = Time.time + 1.3f; //Delay after jump input
}
}
答案 0 :(得分:0)
有些注意事项:
因此,玩家角色恢复其原始速度,因为没有更新该变量。
您需要使用void OnCollision(Collision collision)
方法手动处理碰撞,并根据碰撞数据更新速度变量。
这将要求你的角色拥有RigidBody
,但是为了防止物理引擎与你的玩家控制器的移动(翻译)竞争,该身体将需要运动。