gpt4 book ai didi

android - 统一在android中某些对象上的mouseDown和mouseUp

转载 作者:行者123 更新时间:2023-11-30 01:35:30 28 4
gpt4 key购买 nike

我尝试为 Android 制作一款游戏,您可以在其中按住按钮移动并抬起停止。但是,我无法让它在 android 上运行,因为 OnMouseDown 和 OnMouseUp 仅适用于 PC。有没有办法在android中做到这一点?这是我的代码:

using UnityEngine;
using System.Collections;

public class buttonRight : MonoBehaviour {
public Rigidbody player;
public float speed;
private bool clicking;
void OnMouseDown()
{
Debug.Log("click");
clicking = true;
Vector3 movement = new Vector3(1.0f, 0.0f, 0.0f);
if (clicking == true)
player.velocity = movement * speed;
}
void OnMouseUp()
{
Debug.Log("stop click");
clicking = false;
Vector3 movement = new Vector3(0.0f, 0.0f, 0.0f);
player.velocity = movement * 0;
}
}

最佳答案

一千遍的问题。好吧,答案是 OnMouseDownOnMouseUp 只执行一次,您正尝试将它们用作 OnMouseDown 每帧执行。

你可以这样做,

Vector3 movement = new Vector3 (1.0f, 0.0f, 0.0f);
bool clicking = false;

void Update ()
{
if (clicking == true)
_rb.velocity = movement * 5;
else
_rb.velocity = movement * 0;
}

void OnMouseDown ()
{
Debug.Log ("click");
clicking = true;

}

void OnMouseUp ()
{
Debug.Log ("stop click");
clicking = false;

}

关于android - 统一在android中某些对象上的mouseDown和mouseUp,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35156462/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com