【发布时间】:2019-04-03 16:07:46
【问题描述】:
所以我一直在编写一个脚本,让球在 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);
}
}
【问题讨论】:
-
你能把你的代码格式化得好一点吗?它真的很难阅读,因为它是