Unity 2D Touch Movement
本文为作者原创,转载请注明出处:https://www.cnblogs.com/zhaoqingqing/p/3633725.html
Demo试玩(Kongregate既然也有广告时间了 --!)http://www.kongregate.com/games/zhaoqingqing/2d-touch-movement
操作步骤#
1、下载素材 http://pan.bai du.com/s/1gdkQz8v
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; } } } }
资源下载#
作者:赵青青 一名在【网易游戏】做游戏开发的程序员,擅长Unity3D,游戏开发,.NET等领域。
本文版权归作者和博客园共有,欢迎转载,转载之后请务必在文章明显位置标出原文链接和作者,谢谢。
如果本文对您有帮助,请点击【推荐】您的赞赏将鼓励我继续创作!想跟我一起进步么?那就【关注】我吧。
本文版权归作者和博客园共有,欢迎转载,转载之后请务必在文章明显位置标出原文链接和作者,谢谢。
如果本文对您有帮助,请点击【推荐】您的赞赏将鼓励我继续创作!想跟我一起进步么?那就【关注】我吧。
分类:
uGUI/Unity2D
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 没有源码,如何修改代码逻辑?
· 一个奇形怪状的面试题:Bean中的CHM要不要加volatile?
· [.NET]调用本地 Deepseek 模型
· 一个费力不讨好的项目,让我损失了近一半的绩效!
· .NET Core 托管堆内存泄露/CPU异常的常见思路
· DeepSeek “源神”启动!「GitHub 热点速览」
· 微软正式发布.NET 10 Preview 1:开启下一代开发框架新篇章
· C# 集成 DeepSeek 模型实现 AI 私有化(本地部署与 API 调用教程)
· DeepSeek R1 简明指南:架构、训练、本地部署及硬件要求
· NetPad:一个.NET开源、跨平台的C#编辑器