玩家想要从单点触控左右移动(Unity)

时间:2017-08-02 12:20:44

标签: unity3d unity5

我在Unity Space Shooter中制作了游戏。在我的太空射击游戏中有2个按钮可用于左右移动。我想当我们触摸左键播放器时,只能在单键触摸中向右移动,就像右键一样。

这是我在Game中使用的一些代码。请帮帮我。

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 = "";
//  }
}

3 个答案:

答案 0 :(得分:0)

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的布尔值,当它为真时向右移动,当它为假时向左移动。然后当您单击向左或向右按​​钮时,只需更改变量。

答案 1 :(得分:0)

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

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;
    }
}

然后在播放器控制器的Update方法中使用:

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

答案 2 :(得分:0)

`使用UnityEngine; 公共类HalfScreenTouchMovement:MonoBehaviour { 私人浮动screenCenterX;

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
            }
        }
    }
}

}`