如何解决Unity 3D运动脚本遇到问题
image of the scene. 我在线使用教程,然后在代码无法正常工作时遇到问题。
我的主要问题是isGrounded变量。使用时,它不允许我的精灵跳跃,当它的工作是通过仅允许您在接触地面时跳跃来防止杜宾跳时,一切都与引擎内组件相关联,我认为主要问题在于该行:
public class PlayerMovement : MonoBehaviour
{
public CharacterController controller;
public float speed = 10f;
Vector3 velocity;
public float gravity = -20f;
public Transform groundCheck;
public float groundDistance = 0.4f;
public LayerMask groundMask;
public float jumpHeight = 3f;
public float speedManager = 1.5f;
bool isGrounded;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
isGrounded = Physics.CheckSphere(groundCheck.position,groundDistance,groundMask);
if( isGrounded && velocity.y < 0)
{
velocity.y = -10f;
}
float x = Input.GetAxis("Horizontal");
float z = Input.GetAxis("Vertical");
Vector3 move = transform.right * (x / speedManager) + transform.forward * z;
controller.Move(move * speed * Time.deltaTime);
velocity.y += gravity * Time.deltaTime;
if (Input.GetButtonDown("Jump") && isGrounded)
{
velocity.y = Mathf.Sqrt(jumpHeight * -10f * gravity);
}
controller.Move(velocity * Time.deltaTime);
}
}
其余代码如下。感谢您的阅读。
{
"cause" : {
"type" : "EVT1","payload" : {
"cause_id" : "ABC-123"
}
}
}
解决方法
检查组件中是否设置了public LayerMask groundMask;
,并且地面GameObject的图层与groundMask
相同。
编辑:
您可以将这段代码添加到您的班级中,以检查地面检查是否在正确的位置进行:
private void OnDrawGizmos()
{
Gizmos.color = Color.red;
Gizmos.DrawSphere(groundCheck.position,groundDistance);
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。