我正在为移动设备制作 2D 平台游戏。我有一个 guiTexture,当按下它时,我的精灵会跳跃。但是每当我按下 guiTexture 时,精灵每次都会跳到不同的高度,这取决于我是按住按钮还是轻轻按下它。
这是我正在使用的跳转脚本:
using UnityEngine;
using System.Collections;
public class Jump : MonoBehaviour {
public GUITexture jumpButton;
bool grounded = false;
public Transform groundCheck;
float groundRadius = 0.2f;
public LayerMask whatIsGround;
public float jumpForce = 700f;
public GameObject character;
void Start () {
}
void Update () {
}
void FixedUpdate () {
grounded = Physics2D.OverlapCircle (groundCheck.position, groundRadius, whatIsGround);
if (grounded && jumpButton.HitTest(Input.GetTouch(0).position)) {
rigidbody2D.AddForce(new Vector2(0, jumpForce));
}
}
}