【问题标题】:Android Drag Sprite code Unity 5Android 拖动 Sprite 代码 Unity 5
【发布时间】:2017-06-21 21:53:47
【问题描述】:

我是 Unity 新手,一直在学习如何制作 Captain Blaster 2D 游戏的教程,但是我想将其转换为 Android,我想通过用一根手指在屏幕上拖动玩家来控制玩家不明白我的代码有什么问题,有什么帮助,谢谢

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;

public class ShipControl : MonoBehaviour {

    public float playerSpeed = 10f;
    public GameControl gameController;
    public GameObject bulletPrefab;
    public float reloadTime = 1f;

    private float elapsedTime = 0;


    void Update()
    {

        elapsedTime += Time.deltaTime;
        if (Input.touchCount >= 1)
        {
            foreach (Touch touch in Input.touches) 
            {
                Ray ray = Camera.main.ScreenPointToRay (touch.position);
                RaycastHit hit;
                if (Physics.Raycast (ray, out hit, 100)) {

                }
            }

        if (elapsedTime > reloadTime)
        {
            Vector3 spawnPos = transform.position;
            spawnPos += new Vector3 (0, 1.2f, 0);
            Instantiate (bulletPrefab, spawnPos, Quaternion.identity);

                elapsedTime = 0f;
        }
        }
    }
    void OnTriggerEnter2D(Collider2D other)
    {
        gameController.PlayerDied ();
    }

}

【问题讨论】:

  • 好吧,确定手指实际上触摸某物的代码部分是空的...

标签: c# android unity3d touch


【解决方案1】:

我要做的是添加一个名为“拖动”的布尔值,然后在检查 Raycast 是否击中任何东西后,还要检查被击中的对象是否是玩家游戏对象。 如果它是那么只要用户没有释放触摸 - 让玩家的刚体向触摸位置移动(所以如果有任何障碍物它根本不会穿过它们)。

代码可能看起来像这样(您还应该添加一些计时器来检查玩家是否释放触摸并将拖动 bool 设置为 false):

public float playerSpeed = 10f;
public GameControl gameController;
public GameObject bulletPrefab;
public float reloadTime = 1f;

private float elapsedTime = 0;

private bool dragging = false;


void Update()
{

    if (Input.touchCount >= 1)
    {
        foreach (Touch touch in Input.touches) 
        {
            Ray ray = Camera.main.ScreenPointToRay (touch.position);
            RaycastHit hit;

            if (Physics.Raycast (ray, out hit, 100)) 
            {
                if(hit.collider.tag == "Player") // check if hit collider has Player tag
                {
                    dragging = true;
                }
            }

            if(dragging)
            {
                //First rotate the player towards the touch (should do some checks if it's not too close so it doesn't glitch out)
                Vector3 _dir = Camera.main.ScreenToWorldPoint(touch.position) - transform.position;
                _dir.Normalize();

                float _rotZ = Mathf.Atan2(_dir.y, _dir.x) * Mathf.Rad2Deg;
                transform.rotation = Quaternion.Euler(0f, 0f, _rotZ - 90);

                //Move towards the touch
                transform.GetComponent<Rigidbody>().AddRelativeForce(direction.normalized * playerSpeed, ForceMode.Force);
            }
        }
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-05-18
    • 1970-01-01
    相关资源
    最近更新 更多