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;
}
}
}
}

资源下载

工程下载:http://pan.baidu.com/s/1dDpEkhz

最新文章

  1. .net Session 超时控制
  2. Book LIst
  3. Effective C++ -----条款04:确定对象被使用前已被初始化
  4. NOP登录验证管理
  5. UE4简单AI
  6. thinkphp验证码点击更换js实现
  7. [置顶] Linux信号相关笔记
  8. Hadoop配置项整理(mapred-site.xml)【转】
  9. Unable to connect to your virtual device!解决方法
  10. malloc &amp; free
  11. Eclipse插件引入jar包的方法(转)
  12. C#中数组,ArrayList与List对象的区别
  13. Codeforces Round #452 E. New Year and Old Subsequence
  14. 【python】升级pip后报错解决pkg_resources.DistributionNotFound: The &#39;pip==7.1.0&#39; distribution was not found and is required by the application
  15. jquery与原生JS实现增加、减小字号功能
  16. R语言-线图(二)
  17. JAVA JDK的安装及初步试用
  18. PHP_EOL 写入字符串换行 , php获取毫秒 microtime
  19. springboot 邮件服务
  20. UVA 1611 Crane 起重机 (子问题)

热门文章

  1. 【GPU编解码】GPU硬编码
  2. [Windows Phone] APP上架,遇到错误2001的解决方案。(Error:2001)
  3. playframework文档未提及,但你能做的事
  4. ASP.NET HttpRuntime.Cache缓存类使用总结
  5. PFold.js 折叠纸片
  6. 安卓开发_浅谈ListView(SimpleAdapter数组适配器)
  7. wxPython简单入门
  8. C语言-02-基本运算
  9. iOS开发网络篇—搭建本地服务器(待整理)
  10. 【MySQL】MySQL忘记root密码解决方案