我可以使用虚拟按钮(图像目标的子项)来移动不是图像目标子项的对象吗?
如果是,我该如何编写相同的脚本?
如果不是,还有其他方法吗?
从这张照片中,您将了解我想要做的事情。
基本上,我希望这些虚拟按键在迷宫中移动球
答案 0 :(得分:2)
尝试使用此尺寸。
在您的图像目标上,将此脚本附加到它。 基本上你会使用FindChild或其他东西,而是将游戏对象公开留在课堂上。这样,您可以通过检查器将其直接指定给场景中的任何内容。
和TADAA!
using UnityEngine;
using System.Collections.Generic;
public class VirtualButtonEvent : MonoBehaviour, IVirtualButtonEventHandler {
public GameObject Sphere;
public float speed;
// register buttons for event handling
void Start() {
VirtualButtonBehaviour[] vbs = GetComponentsInChildren<VirtualButtonBehaviour>();
for (int i = 0; i < vbs.Length; ++i) { vbs[i].RegisterEventHandler(this); }
}
// button is "pressed" to move Sphere
public void OnButtonPressed(VirtualButtonAbstractBehaviour vb) {
if (vb.VirtualButtonName=="Up") {
Sphere.rigidbody.AddForce(new Vector3 (0, 0, speed) * speed);
}
if (vb.VirtualButtonName=="Down") {
Sphere.rigidbody.AddForce(new Vector3 (0, 0, -speed) * speed);
}
if (vb.VirtualButtonName=="Left") {
Sphere.rigidbody.AddForce(new Vector3 (-speed, 0, 0) * speed);
}
if (vb.VirtualButtonName=="Right") {
Sphere.rigidbody.AddForce(new Vector3 (speed, 0, 0) * speed);
}
}
// Release to stop Sphere? (Maybe. Don't think this will stop the ball from moving.
public void OnButtonReleased(VirtualButtonAbstractBehaviour vb) {
if (vb.VirtualButtonName=="Up") {
Sphere.rigidbody.AddForce(new Vector3 (0, 0, 0));
}
if (vb.VirtualButtonName=="Down") {
Sphere.rigidbody.AddForce(new Vector3 (0, 0, 0));
}
if (vb.VirtualButtonName=="Left") {
Sphere.rigidbody.AddForce(new Vector3 (0, 0, 0));
}
if (vb.VirtualButtonName=="Right") {
Sphere.rigidbody.AddForce(new Vector3 (0, 0, 0));
}
}
}