我正在编辑Google Chrome的恐龙游戏。我想在手机上运行这款游戏,但我没有添加移动触控功能。我想只为这个游戏的触摸功能。当我触摸屏幕时,它会跳转或开始新游戏。 (我不需要鸭子选项。)我希望你们帮帮我。
using UnityEngine;
using System.Collections;
public class JumpAndDuck : MonoBehaviour
{
public Level level = null;
public GameObject ground = null;
public Collider2D standingCollider = null;
public Collider2D duckingCollider = null;
public AudioSource jumpAudioSource = null;
public AudioClip jumpAudioClip = null;
private Animator animator;
private bool grounded = true;
private bool ducking = false;
private float jumpVelocity = 0f;
private float gravity = 144f;
private Vector3 startVector;
void Start()
{
animator = GetComponent<Animator>();
standingCollider.enabled = true;
duckingCollider.enabled = false;
}
void Update()
{
if (grounded)
{
if (Input.GetButton("Jump") || Input.GetAxis("Vertical") > 0)
{
jump();
}
else if (Input.GetAxis("Vertical") < 0)
{
duck();
}
else
{
stand();
}
}
else
{
transform.position += jumpVelocity * Vector3.up * Time.deltaTime;
jumpVelocity -= gravity * Time.deltaTime;
if (transform.position.y < ground.transform.position.y)
{
grounded = true;
transform.position = startVector;
animator.SetBool("jumping", false);
}
else if (3 < transform.position.y && 20 < jumpVelocity)
{
jumpVelocity = 20;
}
}
}
void OnCollisionEnter2D(Collision2D collision)
{
if (collision.gameObject == ground)
{
grounded = true;
transform.position = startVector;
animator.SetBool("jumping", false);
}
}
void OnCollisionExit2D(Collision2D collision)
{
if (collision.gameObject == ground)
{
grounded = false;
animator.SetBool("jumping", true);
}
}
void jump()
{
if (!grounded)
{
return;
}
stand();
if (jumpAudioSource && jumpAudioClip)
{
jumpAudioSource.PlayOneShot(jumpAudioClip, 1);
}
startVector = transform.position;
jumpVelocity = 40f + level.mainSpeed / 10f;
grounded = false;
animator.SetBool("jumping", true);
}
void duck()
{
if (ducking || !grounded)
{
return;
}
standingCollider.enabled = false;
duckingCollider.enabled = true;
ducking = true;
animator.SetBool("ducking", true);
}
void stand()
{
if (!ducking)
{
return;
}
standingCollider.enabled = true;
duckingCollider.enabled = false;
ducking = false;
animator.SetBool("ducking", false);
}
}
代码和项目不是我的。它通过鼠标点击在PC上运行良好,我需要在具有触摸功能的移动设备上进行。
答案 0 :(得分:0)
您可以使用此代码
using UnityEngine;
using System.Collections;
public class ExampleClass : MonoBehaviour {
void Update() {
if (Input.GetMouseButtonDown(0))
Debug.Log("Pressed left click.");
if (Input.GetMouseButtonDown(1))
Debug.Log("Pressed right click.");
if (Input.GetMouseButtonDown(2))
Debug.Log("Pressed middle click.");
}
}
您需要从Update函数调用此函数,因为每个帧都会重置状态。在用户释放鼠标按钮并再次按下鼠标按钮之前,它不会返回true。按钮值为0表示左按钮,1表示右按钮,2表示中间按钮。
网址:https://docs.unity3d.com/ScriptReference/Input.GetMouseButtonDown.html