简体   繁体   English

Android Drag Sprite代码Unity 5

[英]Android Drag Sprite code Unity 5

I'm new to Unity and have been following a tutorial on how to make a Captain Blaster 2D game, however I want to convert it to Android, I want to make the player controllable by dragging him across the screen with one finger and don't understand what's wrong with my code, anything helps, thanks 我是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 ();
    }

}

What I would do is add a bool called "dragging" and after you check if Raycast hit anything you also check if hit object is the player GameObject. 我要做的是添加一个名为“ dragging”的布尔值,然后检查Raycast是否命中了任何东西,还检查是否命中对象是玩家GameObject。 If it is then as long as user is not releasing the touch - make player's rigidbody move towards the touch position (so if there are any obstacles it simply doesn't move right through them). 如果这样的话,只要用户不释放触摸-让玩家的刚体移向触摸位置(因此,如果有任何障碍,它就不会直接在它们之间移动)。

Code would probably look like this (you should also add some timer to check if player released touch and set dragging bool to false): 代码可能看起来像这样(您还应该添加一些计时器来检查玩家是否释放了触摸并将拖动布尔值设置为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);
            }
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM