首页 > 解决方案 > OnGround Raycast 在缩放 fps 字符后不起作用

问题描述

我正在尝试制作 FPS 游戏。在运动.cs。Raycast 从 Capsule 的中心(玩家的 transform.position)开始,向 Vector3.down 发送,限制为 playerHeight / 2 + 0.1f,其中 playerHeight 是玩家(Capsule)的 CapsuleCollider 的高度。

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class checkJump : MonoBehaviour
{
    private Rigidbody rb;
    private float playerHeight = 2f; 
    private bool isOnGround;
    private float jumpSpeed = 400f;

    void Start() {
        rb = GetComponent<Rigidbody>();
    }
    void Update()
    {
        isOnGround = Physics.Raycast(transform.position, Vector3.down, playerHeight / 2 + 0.1f);
        Jump();
        Debug.Log(isOnGround);
    }

    void Jump() {
        if (Input.GetKeyDown(KeyCode.Space) && isOnGround)
        {
            rb.AddForce(Vector3.up * jumpSpeed * Time.deltaTime, ForceMode.Impulse);
        
        }
    }
}

我禁用了除“checkJump.cs”之外的所有其他脚本。当我将比例值设置为 (4,4,3) 或其他值时,它在控制台中显示为 false,并且当我按下 Enter 时玩家也不会跳转。但是当我将比例值设置为 (1,1,1) 时,它显示为 true。在这两种情况下,玩家都在地面上。

还尝试了另一个 Capsule GameObject,不起作用。

以下是图片:

https://forum.unity.com/attachments/onescale-png.931606/

https://forum.unity.com/attachments/otherscale-png.931609/

标签: c#unity3d

解决方案


仅当 RayCast 撞击地面时,您的 isOnGround 变量才设置为 true。由于您已将其设置为 transform.position,它将从角色的中心发射。您还将它发射的距离限制为 playerHeight / 2 + 0.1,即 1.1。

所以基本上,只要你的 localScale.y 大于 1.1,你的 RayCast 就不再到达地面,因此永远不会记录你在地面上。

要解决这个问题,请将您的 RayCast 距离乘以 localScale.y。


推荐阅读