博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Unity 2D Touch Movement
阅读量:6591 次
发布时间:2019-06-24

本文共 3224 字,大约阅读时间需要 10 分钟。

Demo试玩(Kongregate既然也有广告时间了 --!)

操作步骤

1、下载素材

2、新建三个GUITexture(Joystick)及一个Sprite(Nyan)

 

场景搭建

3、创建背景及Platform(添加BoxCollider2D)

 

TouchControls.cs

4、创建脚本 TouchControls.cs

using UnityEngine;using System.Collections;public class TouchControls : MonoBehaviour {    //gui Textures    public GUITexture guiLeft;    public GUITexture guiRight;    public GUITexture guiJump;    //moement variables    public float moveSpeed = 5f;    public float jumpForce = 50f;    public float maxJumpVelocity = 2f;    private bool moveLeft, moveRight, doJump = false;    // Use this for initialization    void Start () {        }        // Update is called once per frame    void Update () {        //check to see if the screen is being touched        if (Input.touchCount > 0)        {            Touch t = Input.GetTouch(0);//get the touch info            //did the touch active just begin?            if (t.phase == TouchPhase.Began)            {                //are we touching  the left arrow?                if (guiLeft.HitTest(t.position, Camera.main))                {                    Debug.Log("Touching left Control");                    moveLeft = true;                }                if (guiRight.HitTest(t.position, Camera.main))                {                    Debug.Log("Touching right Control");                    moveRight = true;                }                if (guiJump.HitTest(t.position, Camera.main))                {                    Debug.Log("Touching jump Control");                    doJump = true;                }            }            //did the touch end?            if (t.phase == TouchPhase.Ended)            {                doJump = moveLeft = moveRight = false;                rigidbody2D.velocity = Vector2.zero;            }        }        //is the left mouse button down?        if (Input.GetMouseButtonDown(0))        {            if (guiLeft.HitTest(Input.mousePosition, Camera.main))            {                Debug.Log("touching left control");                moveLeft = true;            }            if (guiRight.HitTest(Input.mousePosition, Camera.main))            {                Debug.Log("touching right control");                 moveRight = true;            }            if (guiJump.HitTest(Input.mousePosition, Camera.main))            {                Debug.Log("touching jump control");                doJump = true;            }        }        if (Input.GetMouseButtonUp(0))        {            doJump = moveLeft = moveRight = false;            rigidbody2D.velocity = Vector2.zero;        }    }    void FixedUpdate()    {        if (moveLeft)        {            rigidbody2D.velocity = -Vector2.right * moveSpeed;        }        if (moveRight)        {            rigidbody2D.velocity = Vector2.right * moveSpeed;        }        if (doJump)        {             If we have not reached the maximum jump velocity, keep applying force.            if (rigidbody2D.velocity.y < maxJumpVelocity)            {                rigidbody2D.AddForce(Vector2.up * jumpForce);            }            else            {                //otherwise stop jumping                doJump = false;            }        }    }}

资源下载

工程下载:

转载于:https://www.cnblogs.com/zhaoqingqing/p/3633725.html

你可能感兴趣的文章
DataNode 运行状况
查看>>
牛津词典 2018 年度词汇 ——「有毒」!
查看>>
XIB的是用
查看>>
Learning Data Structure_2_线性表、栈和队列
查看>>
Android Arcface人脸识别sdk使用工具类
查看>>
android studio单个工程文件的代理设置
查看>>
Agent admitted failure to sign using the key
查看>>
grep 应用
查看>>
我的友情链接
查看>>
Linux实验室 CentOS关机大法
查看>>
一行命令获取当前JVM所有可设置的参数以及当前默认值
查看>>
spring与struts2 mvc共存web.xml简单配置
查看>>
Python web爬虫
查看>>
Python捕捉命令输出、错误输出及赋值命令到变量的方法
查看>>
详解性能调优命令
查看>>
Linux mint 14下的powerDNS+mysql+powerAdmin搭建个性DNS域名解析服务器
查看>>
Red Hat EnterPrise Linux 5.4下web服务器的综合使用(普通站点、虚拟主机、安全性、...
查看>>
squirrelmail+change_sqlpass 认证 问题
查看>>
hive优化--增加减少map数
查看>>
重建二叉树
查看>>