Unity 游戏触摸按钮 - 对象只向右移动,不向左移动
Unity game touch button - object only moves right, not left
使用 UI canvas 按钮,我试图在指针向下的情况下左右移动我的对象,当指针向上时,移动应该会停止。但是,只有向右移动的切换情况会执行,而我的对象不会向左移动(但是它会打印语句)
此代码附在左侧按钮上。在指针向下时调用 MoveLeft(),在指针向上时调用 NoLeft()(通过检查器使用事件触发器)。布尔值 isLeft 控制是否向左移动。
public class LeftButton : MonoBehaviour {
public GameObject playerC;
public void MoveLeft(){
Debug.Log ("Moving left");
playerC.GetComponent<PlayerController>().isLeft = true;
}
public void NoLeft(){
Debug.Log ("Not moving left");
playerC.GetComponent<PlayerController> ().isLeft = false;
}
}
下面的代码附在播放器上,我怀疑这是问题所在,我只能向右移动。但是,将打印 isLeft 的日志语句。
public class PlayerController : MonoBehaviour {
private Rigidbody playerRigidBody;
[SerializeField]
public float movementSpeed;
public bool isLeft;
public bool isRight;
void Start () {
playerRigidBody = GetComponent<Rigidbody> ();
}
void FixedUpdate () {
switch (isLeft) {
case true:
print ("Move left is true");
playerRigidBody.MovePosition(transform.position + transform.forward * 0.5f);
break;
case false:
print ("No longer left");
playerRigidBody.MovePosition (transform.position + transform.forward * 0f);
break;
}
switch (isRight) {
case true:
print ("Move right is true");
playerRigidBody.MovePosition (transform.position - transform.forward * 0.5f);
break;
case false:
print ("No longer right");
playerRigidBody.MovePosition (transform.position - transform.forward * 0);
break;
}
}
语句 'No longer right' 也会打印,即使我从未触摸右键并释放它。
如果您想知道 UI 是由两个按钮组成的,左按钮和右按钮。他们都有自己的脚本 LeftButton(上图)和 RightButton,它们只是相互镜像。
在此先感谢您的帮助。
你把它搞得太复杂了,这就是问题所在。简单地有一个方法,它接受一个正数或负数的浮点值。在您的情况下, isLeft 和 isRight 始终为真或假。所以 FixedUpdate 运行s,它会 运行 切换并打印匹配状态。
public class PlayerController : MonoBehaviour
{
public void Move(float polarity) {
playerRigidBody.MovePosition(transform.position + transform.forward * polarity);
}
}
Move 是您分配给两个按钮的方法,然后将极性(1 或 -1)提供给检查器。
使用 UI canvas 按钮,我试图在指针向下的情况下左右移动我的对象,当指针向上时,移动应该会停止。但是,只有向右移动的切换情况会执行,而我的对象不会向左移动(但是它会打印语句)
此代码附在左侧按钮上。在指针向下时调用 MoveLeft(),在指针向上时调用 NoLeft()(通过检查器使用事件触发器)。布尔值 isLeft 控制是否向左移动。
public class LeftButton : MonoBehaviour {
public GameObject playerC;
public void MoveLeft(){
Debug.Log ("Moving left");
playerC.GetComponent<PlayerController>().isLeft = true;
}
public void NoLeft(){
Debug.Log ("Not moving left");
playerC.GetComponent<PlayerController> ().isLeft = false;
}
}
下面的代码附在播放器上,我怀疑这是问题所在,我只能向右移动。但是,将打印 isLeft 的日志语句。
public class PlayerController : MonoBehaviour {
private Rigidbody playerRigidBody;
[SerializeField]
public float movementSpeed;
public bool isLeft;
public bool isRight;
void Start () {
playerRigidBody = GetComponent<Rigidbody> ();
}
void FixedUpdate () {
switch (isLeft) {
case true:
print ("Move left is true");
playerRigidBody.MovePosition(transform.position + transform.forward * 0.5f);
break;
case false:
print ("No longer left");
playerRigidBody.MovePosition (transform.position + transform.forward * 0f);
break;
}
switch (isRight) {
case true:
print ("Move right is true");
playerRigidBody.MovePosition (transform.position - transform.forward * 0.5f);
break;
case false:
print ("No longer right");
playerRigidBody.MovePosition (transform.position - transform.forward * 0);
break;
}
}
语句 'No longer right' 也会打印,即使我从未触摸右键并释放它。
如果您想知道 UI 是由两个按钮组成的,左按钮和右按钮。他们都有自己的脚本 LeftButton(上图)和 RightButton,它们只是相互镜像。
在此先感谢您的帮助。
你把它搞得太复杂了,这就是问题所在。简单地有一个方法,它接受一个正数或负数的浮点值。在您的情况下, isLeft 和 isRight 始终为真或假。所以 FixedUpdate 运行s,它会 运行 切换并打印匹配状态。
public class PlayerController : MonoBehaviour
{
public void Move(float polarity) {
playerRigidBody.MovePosition(transform.position + transform.forward * polarity);
}
}
Move 是您分配给两个按钮的方法,然后将极性(1 或 -1)提供给检查器。