【发布时间】:2021-02-04 15:24:14
【问题描述】:
这很奇怪。
我一直在更深入地了解刚体,但我一辈子都做不到,让它跳起来。我完全复制了我在 youtube 教程上看到的内容,它适用于 youtuber,但不适用于我。
更多 C# 英语 - 我试图通过用户输入在 y 轴上添加 .AddForce(ForceMode.TriedEverything)。它不会始终如一地工作。有时它更高,有时更低,大多数时候什么都没有。这很奇怪。我什至没有设置 isGrounded 或任何其他限制。只需“如果用户输入,则添加强制”。这是第 1 个案例
案例 2 简单得多,但从不工作:它基于将速度设置为仅操纵 y 轴的新向量。我会分享代码。 “jumpForce”设置为 12,但我也试过 500。没有不同。这个案例确实有一个“接地”的条件,但没有它也无法工作。无论如何,两个打印语句都会被执行。
两种情况都在 Update 和 FixedUpdate 中进行了测试,没有区别。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerMotor : MonoBehaviour
{
public LayerMask layerMask = default;
public bool grounded;
[SerializeField] float moveSpeed = 7f;
[SerializeField] float jumpForce = 12f;
Rigidbody rb;
void Start()
{
rb = GetComponent<Rigidbody>();
}
void Update()
{
float x = Input.GetAxisRaw("Horizontal") * moveSpeed;
float y = Input.GetAxisRaw("Vertical") * moveSpeed;
Vector3 movePosition = transform.right * x + transform.forward * y;
Vector3 newPosition = new Vector3(movePosition.x, rb.velocity.y, movePosition.z);
grounded = Physics.CheckSphere(new Vector3(transform.position.x, transform.position.y - 1, transform.position.z), 0.4f, layerMask);
if (Input.GetKeyDown(KeyCode.Space)&& grounded)
{
print("reached if statement");
rb.velocity = new Vector3(rb.velocity.x, jumpForce, rb.velocity.z);
print("jumpingjumping");
}
rb.velocity = newPosition;
}
void FixedUpdate()
{
}
}
谢谢各位。
【问题讨论】: