如何通过点击几下来修复?
How to fix with a few taps?
我有一个角色轮换,就是基于这个。有一个操纵杆。如果您先将手指放在角色的旋转上,然后再放在操纵杆上 - 一切都很好。但是,如果一开始在操纵杆上,然后尝试转动,则什么也不会发生!帮助。几天不能决定
void Start()
{
mContr = GameObject.FindGameObjectWithTag("Joystick").GetComponent<MobileController>();
}
void Update()
{
if(xp > 0)
{
deadStatus = false;
ch_animator.SetBool("dead", false);
}
if(xp <= 0)
{ if(deadStatus == false)
{
menu.gameObject.SetActive(false);
ch_animator.SetBool("Run", false);
ch_animator.SetBool("Walk", false);
ch_animator.SetBool("right", false);
ch_animator.SetBool("left", false);
ch_animator.SetBool("dead", true);
dead.gameObject.SetActive(true);
if(GameObject.Find("selectLvl"))
{
GameObject.Find("selectLvl").SetActive(false);
}
if(GameObject.Find("settings"))
{
GameObject.Find("settings").SetActive(false);
}
deadStatus = true;
}
}
if(eat > 10)
{
if(xp>0&&xp!=100) {
xp += Time.deltaTime * 1f;
eat -= Time.deltaTime * 0.5f;
}
speedMove = 3;
} else {
speedMove = 1;
if(eat < 1 ) {
xp -= Time.deltaTime * 0.06f;
}
}
if(xp.ToString() != hpText.text) {
hpText.text = Math.Round(xp).ToString();
}
if(eat.ToString() != eatText.text)
{
eatText.text = Math.Round(eat).ToString();
}
if(star.ToString() != starText.text)
{
starText.text = star.ToString();
}
if(gun.ToString() != gunText.text)
{
gunText.text = gun.ToString();
}
int k = 1;
while(Input.touchCount > k)
{
Vector2 deltaposition = Vector2.zero;
if(Input.GetTouch(k).position.x > Screen.width / 2 - 100)
{
if(k == 0)
deltaposition = Input.GetTouch(k).deltaPosition;
deltaposition.x /= Screen.dpi;
if(deltaposition.x != 0)
{
float speedX = 3 * deltaposition.x;
player.transform.Rotate(Vector3.up * speedX);
}
}
k++;
}
CharacterMove();
GameGravity();
}
Jostic 代码
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;
public class MobileController: MonoBehaviour, IDragHandler, IPointerUpHandler, IPointerDownHandler
{
private Image jostickBG;
[SerializeField]
private Image jostick;
public Vector2 inputVector;
private void Start()
{
jostickBG = GetComponent<Image>();
jostick = transform.GetChild(0).GetComponent<Image>();
}
public virtual void OnPointerDown(PointerEventData ped)
{
}
public virtual void OnPointerUp(PointerEventData ped)
{
inputVector = Vector2.zero;
jostick.rectTransform.anchoredPosition = Vector2.zero;
}
public virtual void OnDrag(PointerEventData ped)
{
Vector2 pos;
if(RectTransformUtility.ScreenPointToLocalPointInRectangle(jostickBG.rectTransform, ped.position, ped.pressEventCamera, out pos))
{
pos.x = (pos.x / jostickBG.rectTransform.sizeDelta.x);
pos.y = (pos.y / jostickBG.rectTransform.sizeDelta.y);
inputVector = new Vector2(pos.x * 2 - 0, pos.y * 2 - 0);
inputVector = (inputVector.magnitude > 1.0f) ? inputVector.normalized : inputVector;
jostick.rectTransform.anchoredPosition = new Vector2(inputVector.x * (jostickBG.rectTransform.sizeDelta.x / 2), inputVector.y * (jostickBG.rectTransform.sizeDelta.y / 2));
}
}
public float Horizontal()
{
return inputVector.x;
}
public float Vertical()
{
return inputVector.y;
}
}
问题出在 while 循环中:
你的逻辑只适用于轮换玩家的 k=0 和 k=1 的欢乐,你开始进入 while 循环,值为 k=1,如果对于这个值它的测试 if 总是假的轮换球员的位置
if(Input.GetTouch(k).position.x > Screen.width / 2 - 100)
我建议你像这样重构你的代码:
当你有 2 次触摸 k=0 和 k=1 时,为每次触摸找到正确的动作。
我不明白你为什么不进入 k=0 的 while 循环?
我有一个角色轮换,就是基于这个。有一个操纵杆。如果您先将手指放在角色的旋转上,然后再放在操纵杆上 - 一切都很好。但是,如果一开始在操纵杆上,然后尝试转动,则什么也不会发生!帮助。几天不能决定
void Start()
{
mContr = GameObject.FindGameObjectWithTag("Joystick").GetComponent<MobileController>();
}
void Update()
{
if(xp > 0)
{
deadStatus = false;
ch_animator.SetBool("dead", false);
}
if(xp <= 0)
{ if(deadStatus == false)
{
menu.gameObject.SetActive(false);
ch_animator.SetBool("Run", false);
ch_animator.SetBool("Walk", false);
ch_animator.SetBool("right", false);
ch_animator.SetBool("left", false);
ch_animator.SetBool("dead", true);
dead.gameObject.SetActive(true);
if(GameObject.Find("selectLvl"))
{
GameObject.Find("selectLvl").SetActive(false);
}
if(GameObject.Find("settings"))
{
GameObject.Find("settings").SetActive(false);
}
deadStatus = true;
}
}
if(eat > 10)
{
if(xp>0&&xp!=100) {
xp += Time.deltaTime * 1f;
eat -= Time.deltaTime * 0.5f;
}
speedMove = 3;
} else {
speedMove = 1;
if(eat < 1 ) {
xp -= Time.deltaTime * 0.06f;
}
}
if(xp.ToString() != hpText.text) {
hpText.text = Math.Round(xp).ToString();
}
if(eat.ToString() != eatText.text)
{
eatText.text = Math.Round(eat).ToString();
}
if(star.ToString() != starText.text)
{
starText.text = star.ToString();
}
if(gun.ToString() != gunText.text)
{
gunText.text = gun.ToString();
}
int k = 1;
while(Input.touchCount > k)
{
Vector2 deltaposition = Vector2.zero;
if(Input.GetTouch(k).position.x > Screen.width / 2 - 100)
{
if(k == 0)
deltaposition = Input.GetTouch(k).deltaPosition;
deltaposition.x /= Screen.dpi;
if(deltaposition.x != 0)
{
float speedX = 3 * deltaposition.x;
player.transform.Rotate(Vector3.up * speedX);
}
}
k++;
}
CharacterMove();
GameGravity();
}
Jostic 代码
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;
public class MobileController: MonoBehaviour, IDragHandler, IPointerUpHandler, IPointerDownHandler
{
private Image jostickBG;
[SerializeField]
private Image jostick;
public Vector2 inputVector;
private void Start()
{
jostickBG = GetComponent<Image>();
jostick = transform.GetChild(0).GetComponent<Image>();
}
public virtual void OnPointerDown(PointerEventData ped)
{
}
public virtual void OnPointerUp(PointerEventData ped)
{
inputVector = Vector2.zero;
jostick.rectTransform.anchoredPosition = Vector2.zero;
}
public virtual void OnDrag(PointerEventData ped)
{
Vector2 pos;
if(RectTransformUtility.ScreenPointToLocalPointInRectangle(jostickBG.rectTransform, ped.position, ped.pressEventCamera, out pos))
{
pos.x = (pos.x / jostickBG.rectTransform.sizeDelta.x);
pos.y = (pos.y / jostickBG.rectTransform.sizeDelta.y);
inputVector = new Vector2(pos.x * 2 - 0, pos.y * 2 - 0);
inputVector = (inputVector.magnitude > 1.0f) ? inputVector.normalized : inputVector;
jostick.rectTransform.anchoredPosition = new Vector2(inputVector.x * (jostickBG.rectTransform.sizeDelta.x / 2), inputVector.y * (jostickBG.rectTransform.sizeDelta.y / 2));
}
}
public float Horizontal()
{
return inputVector.x;
}
public float Vertical()
{
return inputVector.y;
}
}
问题出在 while 循环中:
你的逻辑只适用于轮换玩家的 k=0 和 k=1 的欢乐,你开始进入 while 循环,值为 k=1,如果对于这个值它的测试 if 总是假的轮换球员的位置
if(Input.GetTouch(k).position.x > Screen.width / 2 - 100)
我建议你像这样重构你的代码:
当你有 2 次触摸 k=0 和 k=1 时,为每次触摸找到正确的动作。
我不明白你为什么不进入 k=0 的 while 循环?