帮我写一个unity二段跳代码
时间: 2023-03-27 16:02:30 浏览: 130
好的,以下是一个简单的 Unity 二段跳代码:
public class PlayerController : MonoBehaviour {
public float jumpForce = 10f;
public float secondJumpForce = 8f;
public float jumpTime = .5f;
public float groundCheckRadius = .2f;
public LayerMask groundLayer;
public Transform groundCheck;
private bool isGrounded;
private bool canSecondJump;
private float jumpTimeCounter;
void Update() {
isGrounded = Physics2D.OverlapCircle(groundCheck.position, groundCheckRadius, groundLayer);
if (isGrounded) {
canSecondJump = true;
}
if (Input.GetKeyDown(KeyCode.Space)) {
if (isGrounded) {
Jump();
} else if (canSecondJump) {
Jump(secondJumpForce);
canSecondJump = false;
}
}
if (Input.GetKey(KeyCode.Space)) {
if (jumpTimeCounter > ) {
Jump();
jumpTimeCounter -= Time.deltaTime;
}
}
if (Input.GetKeyUp(KeyCode.Space)) {
jumpTimeCounter = ;
}
}
void Jump(float force = ) {
GetComponent<Rigidbody2D>().velocity = new Vector2(GetComponent<Rigidbody2D>().velocity.x, force == ? jumpForce : force);
jumpTimeCounter = jumpTime;
}
void OnDrawGizmosSelected() {
Gizmos.color = Color.red;
Gizmos.DrawWireSphere(groundCheck.position, groundCheckRadius);
}
}
阅读全文