因此,我一直在研究脚本,以使球以恒定的速度在X轴上移动,但也可以控制。当我按空格键跳。除非我按5至6次按钮,否则它不会跳。第一次跳球后,如果反复轻按空格键,球会跳起来,但是如果不理会球滚动,然后尝试再次跳球。它不会让你。我很困惑。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Speed : MonoBehaviour
{
public float sspeed = 7.0F;
public float jumpSpeed = 8.0F;
public float gravity = 20.0F;
private Vector2 moveDirection = Vector2.zero;
void Start()
{
}
void Update()
{
if (GetComponent<CharacterController>().isGrounded)
{
moveDirection = new Vector2();
moveDirection = transform.TransformDirection(moveDirection);
moveDirection *= sspeed;
moveDirection.x = sspeed;
}
else if (GetComponent<CharacterController>())
{
moveDirection.x = gravity;
}
moveDirection.x -= gravity * Time.deltaTime;
GetComponent<CharacterController>().Move(moveDirection * Time.deltaTime);
CharacterController player = GetComponent<CharacterController>();
if (GetComponent<CharacterController>().isGrounded)
{
moveDirection = new Vector2();
moveDirection = transform.TransformDirection(moveDirection);
moveDirection *= sspeed;
if (Input.GetKeyDown("space"))
{
moveDirection.y = jumpSpeed;
}
}
moveDirection.y -= gravity * Time.deltaTime;
GetComponent<CharacterController>().Move(moveDirection * Time.deltaTime);
}
}
答案 0 :(得分:1)
有一种更好的方法:
Rigidbody body = GetComponent<Rigidbody>();
if(Input.GetKeyDown(KeyCode.Space)){
body.AddForce(transform.up*jumpSpeed);
}
答案 1 :(得分:0)
尝试这个
:Gwrite