首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >玩家希望从一次触摸(统一)向左和右移动

玩家希望从一次触摸(统一)向左和右移动
EN

Stack Overflow用户
提问于 2017-08-02 12:20:43
回答 4查看 2K关注 0票数 0

我在联合太空射击队做了这个游戏。在我的空间射击有2个按钮,它工作的左和右移动。我想当我们点击左边按钮的时候,玩家只能在单一的触摸中向左走,就像右键一样。

这是我在游戏中使用的一些密码。请帮我摆脱这一切。

TouchControl.cs

代码语言:javascript
复制
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

代码语言:javascript
复制
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 = "";
//  }
}
EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2017-08-04 18:43:36

在Player Controller脚本中创建:

代码语言:javascript
复制
public Vector3 playerDirection = Vector3.zero;

然后用触控代替:

代码语言:javascript
复制
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();
    }
}

使用:

代码语言:javascript
复制
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的更新方法中使用:

代码语言:javascript
复制
transform.Translate(playerDirection * speedMove * Time.deltaTime);
票数 0
EN

Stack Overflow用户

发布于 2017-08-02 13:56:05

代码语言:javascript
复制
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的布尔值,当它是真时向右移动,当它是假时向左移动。然后,单击“左”或“右”按钮,只需更改变量。

票数 0
EN

Stack Overflow用户

发布于 2020-04-12 11:12:55

`使用UnityEngine;公共类HalfScreenTouchMovement : MonoBehaviour {私有浮点数screenCenterX;

代码语言:javascript
复制
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
            }
        }
    }
}

}`

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/45460742

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档