我正在做一个游戏在团结的基础上,在这个教程https://www.youtube.com/watch?v=rJqP5EesxLk&t=139s,但我的性格不断跳跃半空这里我的代码;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerMotor : MonoBehaviour
{
private CharacterController controller;
private Vector3 playerVelocity;
private bool isGrounded;
public float speed = 5f;
public float gravity = -9.8f;
public float jumpHeight = 3f;
// Start is called before the first frame update
void Start()
{
controller = GetComponent<CharacterController>();
}
// Update is called once per frame
void Update()
{
isGrounded = controller.isGrounded;
}
//Receber os inputs e aplicar no controle
public void ProcessMove(Vector2 input)
{
Vector3 moveDirection = Vector3.zero;
moveDirection.x = input.x;
moveDirection.z = input.y;
controller.Move(transform.TransformDirection(moveDirection) * speed * Time.deltaTime);
playerVelocity.y += gravity * Time.deltaTime;
if(isGrounded && playerVelocity.y < 0)
{
playerVelocity.y = -2f;
}
controller.Move(playerVelocity * Time.deltaTime);
Debug.Log(playerVelocity.y);
}
public void Jump()
{
playerVelocity.y = Mathf.Sqrt(jumpHeight * -3.0f * gravity);
}
}
”
我尝试创建一个if函数if(isGrounded)
1条答案
按热度按时间yeotifhr1#
试试这个: