【问题标题】:Player want to move left and right from single - touch (Unity)玩家想要从单点触摸左右移动(Unity)
【发布时间】:2021-08-13 04:04:46
【问题描述】:

我在 Unity Space Shooter 中制作了这款游戏。在我的太空射击游戏中,有 2 个按钮可用于左右移动。我希望当我们触摸左键时,玩家也只能像右键一样在 Single Touch 中向左移动。

这是我在游戏中使用的一些代码。请帮我解决这个问题。

TouchControl.cs

using UnityEngine;
using System.Collections;

public class TouchControl : MonoBehaviour {

    public GUITexture moveLeft;
    public GUITexture moveRight;
    public GUITexture fire;
    public GameObject player;
    private PlayerMovement playerMove;
    private Weapon[] weapons;

    void Start()
    {
        playerMove = player.GetComponent<PlayerMovement> ();
    }

    void CallFire()
    {
        weapons = player.GetComponentsInChildren<Weapon> ();
        foreach (Weapon weapon in weapons) {
            if(weapon.enabled == true)
            weapon.Fire();      
        }
    }

    void Update()
    {
//      int i = 0;
        if(Input.touchCount > 0)
        {
            for(int i =0; i < Input.touchCount; i++)
            {
//          if(moveLeft.HitTest(Input.GetTouch(i).position, Camera.main))
//          {
//              if(Input.touchCount > 0)
//              {
//                  playerMove.MoveLeft();
//              }
//          }
//          if(moveRight.HitTest(Input.GetTouch(i).position, Camera.main))
//          {
//              if(Input.touchCount > 0)
//              {
//                  playerMove.MoveRight();
//              }
//          }
//          if(moveLeft.HitTest(Input.GetTouch(i).position, Camera.main))
//          {
//              if(Input.touchCount > 0)
//              {
//                  CallFire();
//              }
//          }
            //  Touch t = Input.GetTouch(i);
                Touch t = Input.GetTouch (i);
                Input.multiTouchEnabled = true;
                if(t.phase == TouchPhase.Began || t.phase == TouchPhase.Stationary)
                {
                    if(moveLeft.HitTest(t.position, Camera.main))
                    {
                       playerMove.MoveLeft ();
                }
                    if(moveRight.HitTest(t.position, Camera.main))
                {
                        playerMove.MoveRight();
                    }
                }
                if(t.phase == TouchPhase.Began)
                {
                    if(fire.HitTest(t.position, Camera.main))
                    {
                        CallFire();
                    }
                }


                if(t.phase == TouchPhase.Ended)
                {

                }
            }
        }
    }
}

PlayerMovement.cs

using UnityEngine;
using System.Collections;

public class PlayerMovement : MonoBehaviour {
    public float speedMove = 6.0f;
    public float bonusTime;

    private bool toLeft = false;
    private bool toRight = false;

    public GameObject shield;
    public GUIText bonustimeText;

    private bool counting = false;
    private float counter;

    private Weapon[] addWeapons;

    public Sprite strongShip;
    public Sprite normalSprite;
    public Sprite shieldSprite;

    private SpriteRenderer sRender;
    private Weapon weaponScript;

    void Start () {

        counter = bonusTime;

        sRender = GetComponent<SpriteRenderer> ();
        addWeapons = GetComponentsInChildren<Weapon> ();
        foreach (Weapon addWeapon in addWeapons) {
            addWeapon.enabled = false;
        }

        weaponScript = GetComponent<Weapon>();
        weaponScript.enabled = true;
    }

    // Update is called once per frame
    void Update () {

        if (Input.GetKeyDown (KeyCode.A)) {
            toLeft = true;      
        }
        if (Input.GetKeyUp (KeyCode.A)) {
            toLeft = false;     
        }
        if (Input.GetKeyDown (KeyCode.D)) {
            toRight = true;     
        }
        if (Input.GetKeyUp (KeyCode.D)) {
            toRight = false;        
        }


        if (counting) {
            counter -= Time.deltaTime;
            bonustimeText.text = counter.ToString("#0.0");
        }
    }


    void FixedUpdate()
    {
        if (toLeft) {
            MoveLeft();
        }

        if (toRight) {  
            MoveRight();
        }
    }


    public void MoveLeft()
    {
        transform.Translate(Vector2.right * -speedMove* Time.deltaTime);
    }


    public void MoveRight()
    {
        transform.Translate(Vector2.right * speedMove * Time.deltaTime);
    }


    void OnCollisionEnter2D(Collision2D coll)
    {
        if (coll.gameObject.tag == "StrongMode") {
            Destroy (coll.gameObject);
            counting = true;
            StrongMode();
            Invoke ("Downgrade", bonusTime);
        }


        if (coll.gameObject.tag == "ShieldMode") {
            Destroy (coll.gameObject);
            counting = true;
            ShieldMode();
            Invoke("Downgrade", bonusTime);
        }

        if (coll.gameObject.tag == "Life") {
            GUIHealth gui = GameObject.Find ("GUI").GetComponent<GUIHealth> ();
            gui.AddHealth();
            SendMessage("AddHp");
            SoundHelper.instanceSound.PickUpSound();
            Destroy(coll.gameObject);
        }

        if (coll.gameObject.tag == "Enemy") {
            SendMessage("Dead");
        }
    }

    void Downgrade()
    {
        SoundHelper.instanceSound.BonusDownSound ();
        counting = false;
        bonustimeText.text = "";
        counter = bonusTime;

        sRender.sprite = normalSprite;
        weaponScript.enabled = true;
        foreach (Weapon addWeapon in addWeapons) {
            addWeapon.enabled = false;
        }
        weaponScript.enabled = true;
        shield.SetActive (false);
    }


    void StrongMode()
    {
        SoundHelper.instanceSound.BonusUpSound ();
        sRender.sprite = strongShip;
        foreach (Weapon addWeapon in addWeapons) {
            addWeapon.enabled = true;
        }
        weaponScript.enabled = false;
    }


    void ShieldMode()
    {
        SoundHelper.instanceSound.BonusUpSound ();
        sRender.sprite = shieldSprite;
        shield.SetActive (true);
    }


//  void OnDestroy()
//  {
//      bonustimeText.text = "";
//  }
}

【问题讨论】:

  • 所以你有左右按钮,哪个有效?如果用户触摸屏幕的右侧或左侧部分,您想禁用按钮并进行移动?
  • 是的,我有一个左右按钮,但我想多次单击该按钮而不是播放器左右移动。我只想要一次我按下右侧按钮它转到右侧。

标签: unity3d unity5


【解决方案1】:

在播放器控制器脚本中创建:

public Vector3 playerDirection = Vector3.zero;

然后在触摸控制中而不是:

if (moveLeft.HitTest(Input.GetTouch(i).position, Camera.main))
{
    if (Input.touchCount > 0)
    {
        playerMove.MoveLeft();
    }
}
if (moveRight.HitTest(Input.GetTouch(i).position, Camera.main))
{
    if (Input.touchCount > 0)
    {
        playerMove.MoveRight();
    }
}

用途:

if (moveLeft.HitTest(Input.GetTouch(i).position, Camera.main))
{
    if (Input.touchCount > 0)
    {
        playerMove.playerDirection = Vector3.left;
    }
}
if (moveRight.HitTest(Input.GetTouch(i).position, Camera.main))
{
    if (Input.touchCount > 0)
    {
        playerMove.playerDirection = Vector3.right;
    }
}

然后在Player Controller的Update方法中使用:

transform.Translate(playerDirection * speedMove * Time.deltaTime);

【讨论】:

  • 我使用了你给我的任何代码,但我的播放器甚至没有移动。 @Alexandru
【解决方案2】:
public class PlayerController {
    public EPlayerState playerState = EPLayerState.Idle;

    void Update () {
      // If click right button
      playerState = EPlayerState.MoveRight;
      // Else if click left button
      playerState = EPlayerState.MoveLeft

      if (playerState == EPlayerState.MoveRight)
          // Move player right;
      if (playerState == EPlayerState.MoveLeft
          // Move player right;
    }
}

public enum EPlayerState {
    Idle,
    MoveRight,
    MoveLeft
}

您还可以使用类似于布尔值的东西,称为 isRight,当它为真时向右移动,当它为假时向左移动。然后,当您单击左键或右键时,只需更改变量即可。

【讨论】:

    【解决方案3】:

    `使用UnityEngine; 公共类 HalfScreenTouchMovement : MonoBehaviour { 私有浮动屏幕CenterX;

    private void Start()
    {
        // save the horizontal center of the screen
        screenCenterX = Screen.width * 0.5f;
    }
    
    private void Update()
    {
        // if there are any touches currently
        if(Input.touchCount > 0)
        {
            // get the first one
            Touch firstTouch = Input.GetTouch(0);
    
            // if it began this frame
            if(firstTouch.phase == TouchPhase.Began)
            {
                if(firstTouch.position.x > screenCenterX)
                {
                    // if the touch position is to the right of center
                    // move right
                }
                else if(firstTouch.position.x < screenCenterX)
                {
                    // if the touch position is to the left of center
                    // move left
                }
            }
        }
    }
    

    }`

    【讨论】:

      【解决方案4】:

      可能会有所帮助

      1. 创建脚本。例如 player.cs

        public class PlayerController : MonoBehaviour
         {
              bool  swipeRight = false;
              bool  swipeLeft = false;
              bool touchBlock = true;
              bool canTouchRight = true;
              bool canTouchLeft = true;
        
             void Update()
             {
                   swipeLeft = Input.GetKeyDown("a") || Input.GetKeyDown(KeyCode.LeftArrow);
                   swipeRight = Input.GetKeyDown("d") || Input.GetKeyDown(KeyCode.RightArrow);
                   TouchControl();    
                  if(swipeRight)  //rightMove logic
                  else if(swipeLeft) //leftMove logic
            }
        
           void TouchControl()
           {
        
                if (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Moved && touchBlock == true) 
               {
                      touchBlock = false;
                     // Get movement of the finger since last frame
                     var touchDeltaPosition = Input.GetTouch(0).deltaPosition;
                     Debug.Log("touchDeltaPosition "+touchDeltaPosition);
        
                     if(touchDeltaPosition.x > 0 && canTouchRight == true)
                    {
                         //rightMove
                         swipeRight = true; canTouchRight = false;
                          Invoke("DisableSwipeRight",0.2f);
                      }
                      else  if(touchDeltaPosition.x < 0 && canTouchLeft == true)
                     {
                          //leftMove
                          swipeLeft = true; canTouchLeft = false;
                          Invoke("DisableSwipeLeft",0.2f);
                       }       
                 } 
         }
        void DisableSwipeLeft()
         {
             swipeLeft = false;
             touchBlock = true;
             canTouchLeft = true;
         }
         void DisableSwipeRight()
         {
             swipeRight = false;
             touchBlock = true;
             canTouchRight = true;
         }
         }
        

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-01-11
        • 1970-01-01
        相关资源
        最近更新 更多