角色在unity中无法跳跃


using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Player : MonoBehaviour
{ 
CharacterController characterController;
[SerializeField] private float speed = 5.0f;
public float jumpSpeed = 8.0f;
public float gravity = 20.0f;
private Vector3 moveDirection = Vector3.zero;
private void Start()
{
characterController = GetComponent<CharacterController>();
}
private void Update()
{
var horizontal = Input.GetAxis("Horizontal");
var vertical = Input.GetAxis("Vertical");
transform.Translate(new Vector3(horizontal, 0, vertical) * (speed * Time.deltaTime));
if (characterController.isGrounded)
{
moveDirection = new Vector3(Input.GetAxis("Horizontal"), 0.0f, Input.GetAxis("vertical"));
moveDirection *= speed;
if (Input.GetButton("Jump"))
{
moveDirection.y = jumpSpeed;
}
}
moveDirection.y -= gravity * Time.deltaTime;
characterController.Move(moveDirection * Time.deltaTime);
}

}

当我制作游戏时,我是游戏开发的新手,但我遇到了角色无法跳跃的问题,当我试图移动角色时,它会一直漂浮在空中。

这里的代码为我工作

if (Input.GetButtonDown("Jump"))
{
rb.velocity = new Vector3(rb.velocity.x, jumpHeight, 0);
//jumpheight is set at 14, and my gravity scale is at 4
}

你必须引用你的'rb',如:

public Rigidbody2D rb;然后将玩家的刚体拖拽到脚本上显示的新Rb中。

rb。Velocity在原x速度的基础上增加一个速度,y速度设为14,z速度设为0。

希望这能回答你的问题

最新更新