简体   繁体   English

如何在Unity C#中将对Android OS的触摸支持添加到播放器脚本中?

[英]How can I add touch support for android OS to my player script in unity c#?

I need to add touch controls in my game, I don't know how to do it. 我需要在游戏中添加触摸控件,但我不知道该怎么做。 In my game time scale is very important I read a lot of tutorials and this don't work. 在我的游戏中,时间比例非常重要,我阅读了很多教程,但这不起作用。 I think about something like half screen left and half screen right the map is road but I think this is not important. 我考虑的是左右半屏,右半屏是地图,但这并不重要。 Help me please. 请帮帮我。

This is game like infinite runner. 这就像无限的亚军游戏。 I am very low on scripting. 我的脚本编写能力很低。

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerBehaviour : MonoBehaviour {
public float death =0f;
public float timee;
public float points = 0f;
public float distance = 0f;
void Start ()
{
    timee = Time.timeScale;
}




void Update ()
{

    if (Input.GetKey(KeyCode.A) && transform.position.x <12.5f && transform.position.x >-2.5f)
    {
        GameObject.Find("player").transform.position -= new Vector3((Time.deltaTime * 3.5f )* timee ,0, 0);
    }
    if (Input.GetKey(KeyCode.D) && transform.position.x < 2.5f && transform.position.x > -12.5f)
    {
        GameObject.Find("player").transform.position += new Vector3((Time.deltaTime * 3.5f) * timee , 0, 0);
    } 
    distance = distance + 15 * Time.deltaTime;
}
public float score;
void OnCollisionEnter2D(Collision2D col)
{
    if (col.gameObject.tag == "siema") 
    {
        if (score < 33)
        {
            score++;
            Time.timeScale = 1f+ score * 0.03f;
            timee = 1f - (0.04f * score);
            if (timee < 0.5f && score <= 33)
            {
                timee = 0.5f;
            }
            if (score > 29) 
            {
                timee = 0.4f;
            }
        }
    }

        if (col.gameObject.tag == "base") 
        {
            points = points + score * score;
            score = 0f;
        }
        if (score == 0) 
        {
            Texture texture = Resources.Load<Texture> ("0");
            GameObject.Find ("player").GetComponent<Renderer> ().material.mainTexture = texture;
            Time.timeScale = 1f;
            timee = 1f;
        }
        if (score == 1) {
            Texture texture = Resources.Load<Texture> ("1");
            GameObject.Find ("player").GetComponent<Renderer> ().material.mainTexture = texture;

        }
        if (score == 2) {
            Texture texture = Resources.Load<Texture> ("2");
            GameObject.Find ("player").GetComponent<Renderer> ().material.mainTexture = texture;


        }
        if (score == 3) {
            Texture texture = Resources.Load<Texture> ("3");
            GameObject.Find ("player").GetComponent<Renderer> ().material.mainTexture = texture;

        }
        if (score == 4) {
            Texture texture = Resources.Load<Texture> ("4");
            GameObject.Find ("player").GetComponent<Renderer> ().material.mainTexture = texture;
        }
        if (score == 5) {
            Texture texture = Resources.Load<Texture> ("5");
            GameObject.Find ("player").GetComponent<Renderer> ().material.mainTexture = texture;
        }
        if (score == 6) {
            Texture texture = Resources.Load<Texture> ("6");
            GameObject.Find ("player").GetComponent<Renderer> ().material.mainTexture = texture;
        }
        if (score == 7) {
            Texture texture = Resources.Load<Texture> ("7");
            GameObject.Find ("player").GetComponent<Renderer> ().material.mainTexture = texture;
        }
        if (score == 8) {
            Texture texture = Resources.Load<Texture> ("8");
            GameObject.Find ("player").GetComponent<Renderer> ().material.mainTexture = texture;
        }
        if (score == 9) {
            Texture texture = Resources.Load<Texture> ("9");
            GameObject.Find ("player").GetComponent<Renderer> ().material.mainTexture = texture;
        }
        if (score == 10) {
            Texture texture = Resources.Load<Texture> ("10");
            GameObject.Find ("player").GetComponent<Renderer> ().material.mainTexture = texture;
        }
        if (score == 11) {
            Texture texture = Resources.Load<Texture> ("11");
            GameObject.Find ("player").GetComponent<Renderer> ().material.mainTexture = texture;
        }
        if (score == 12) {
            Texture texture = Resources.Load<Texture> ("12");
            GameObject.Find ("player").GetComponent<Renderer> ().material.mainTexture = texture;
        }
        if (score == 13) {
            Texture texture = Resources.Load<Texture> ("13");
            GameObject.Find ("player").GetComponent<Renderer> ().material.mainTexture = texture;
        }
        if (score == 14) {
            Texture texture = Resources.Load<Texture> ("14");
            GameObject.Find ("player").GetComponent<Renderer> ().material.mainTexture = texture;
        }
        if (score == 15) {
            Texture texture = Resources.Load<Texture> ("15");
            GameObject.Find ("player").GetComponent<Renderer> ().material.mainTexture = texture;
        }
        if (score == 16) {
            Texture texture = Resources.Load<Texture> ("16");
            GameObject.Find ("player").GetComponent<Renderer> ().material.mainTexture = texture;
        }
        if (score == 17) {
            Texture texture = Resources.Load<Texture> ("17");
            GameObject.Find ("player").GetComponent<Renderer> ().material.mainTexture = texture;
        }
        if (score == 18) {
            Texture texture = Resources.Load<Texture> ("18");
            GameObject.Find ("player").GetComponent<Renderer> ().material.mainTexture = texture;
        }
        if (score == 19) {
            Texture texture = Resources.Load<Texture> ("19");
            GameObject.Find ("player").GetComponent<Renderer> ().material.mainTexture = texture;
        }
        if (score == 20) {
            Texture texture = Resources.Load<Texture> ("20");
            GameObject.Find ("player").GetComponent<Renderer> ().material.mainTexture = texture;
        }
        if (score == 21) {
            Texture texture = Resources.Load<Texture> ("21");
            GameObject.Find ("player").GetComponent<Renderer> ().material.mainTexture = texture;
        }
        if (score == 22) {
            Texture texture = Resources.Load<Texture> ("22");
            GameObject.Find ("player").GetComponent<Renderer> ().material.mainTexture = texture;
        }
        if (score == 23) {
            Texture texture = Resources.Load<Texture> ("23");
            GameObject.Find ("player").GetComponent<Renderer> ().material.mainTexture = texture;
        }
        if (score == 24) {
            Texture texture = Resources.Load<Texture> ("24");
            GameObject.Find ("player").GetComponent<Renderer> ().material.mainTexture = texture;
        }
        if (score == 25) {
            Texture texture = Resources.Load<Texture> ("25");
            GameObject.Find ("player").GetComponent<Renderer> ().material.mainTexture = texture;
        }
        if (score == 26) {
            Texture texture = Resources.Load<Texture> ("26");
            GameObject.Find ("player").GetComponent<Renderer> ().material.mainTexture = texture;
        }
        if (score == 27) {
            Texture texture = Resources.Load<Texture> ("27");
            GameObject.Find ("player").GetComponent<Renderer> ().material.mainTexture = texture;
        }
        if (score == 28) {
            Texture texture = Resources.Load<Texture> ("28");
            GameObject.Find ("player").GetComponent<Renderer> ().material.mainTexture = texture;
        }   
        if (score == 29) {
            Texture texture = Resources.Load<Texture> ("29");
            GameObject.Find ("player").GetComponent<Renderer> ().material.mainTexture = texture;
        }   
        if (score == 30) {
            Texture texture = Resources.Load<Texture> ("30");
            GameObject.Find ("player").GetComponent<Renderer> ().material.mainTexture = texture;
        }
        if (score == 31) {
            Texture texture = Resources.Load<Texture> ("31");
            GameObject.Find ("player").GetComponent<Renderer> ().material.mainTexture = texture;
        }   
        if (score == 32) {
            Texture texture = Resources.Load<Texture> ("32");
            GameObject.Find ("player").GetComponent<Renderer> ().material.mainTexture = texture;
        }   
        if (score == 33) {
            Texture texture = Resources.Load<Texture> ("33");
            GameObject.Find ("player").GetComponent<Renderer> ().material.mainTexture = texture;
        }
    if (col.gameObject.tag == "siema") 
    {
        Destroy (col.gameObject);
    }
    if (col.gameObject.tag == "blocade")
    {
        if (score > 0)
        {
            score = 0;
            Texture texture = Resources.Load<Texture> ("0");
            GameObject.Find ("player").GetComponent<Renderer> ().material.mainTexture = texture;
            Time.timeScale = 1f;
            timee = 1f;
            Destroy (col.gameObject);
        } 
        death++;
        Destroy (col.gameObject);
        if (death == 2)
        {
            Time.timeScale = 0.0001f;
        }
    }
}
}

如果您的意思是操纵杆,则可以从Unity标准资产中添加它-CrossPlatformInput-预制-移动单杆控件

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

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