c# - 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,不起作用。
以下是图片:
解决方案
仅当 RayCast 撞击地面时,您的 isOnGround 变量才设置为 true。由于您已将其设置为 transform.position,它将从角色的中心发射。您还将它发射的距离限制为 playerHeight / 2 + 0.1,即 1.1。
所以基本上,只要你的 localScale.y 大于 1.1,你的 RayCast 就不再到达地面,因此永远不会记录你在地面上。
要解决这个问题,请将您的 RayCast 距离乘以 localScale.y。
推荐阅读
- android - 如何使用意图传递接口
- php - 如何按顺序从三个嵌套的 foreach 循环中填充数据?
- python - 将 R 转换为 Python 函数
- python - 无法在openpyxl中保存excel电子表格 - Python
- vba - 定位段落结尾的垂直位置
- html - 将带有子菜单项的块相对于父菜单项居中
- asp.net-core - 如何在 asp.net core mvc 中将客户服务与控制器解耦
- javascript - How to make a global variable or be able to access it in other files
- javascript - React Native Expo - 将 TextInput 放在 Modal 上时出现异常
- linux - GDB 预制手表和命令