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

Unity 2D Touch Movement的更多相关文章

  1. unity 2d 和 NGUI layer

    http://blog.csdn.net/xtxy/article/details/37876825 在使用unity2d开发游戏的时候,使用了NGUI作为界面,本来二者配合得还挺好,但是一个使用场景 ...

  2. Ubuntu 11.10 安装GMONE3,卸载 UNITY和UNITY 2D

    Ubuntu 11.10安装GNOME3: 1)sudo apt-get install gnome-shell    sudo apt-get install gnome-themes*   (或者 ...

  3. Mastering Unity 2D Game Development

    Mastering Unity 2D Game Development will give your game development skills a boost and help you begi ...

  4. Unity 2D游戏开发教程之精灵的死亡和重生

    Unity 2D游戏开发教程之精灵的死亡和重生 精灵的死亡和重生 目前为止,游戏项目里的精灵只有Idle和Walking这两种状态.也就是说,无论精灵在游戏里做什么,它都不会进入其它的状态,如死亡.于 ...

  5. Unity 2D游戏开发教程之摄像头追踪功能

    Unity 2D游戏开发教程之摄像头追踪功能 上一章,我们创建了一个简单的2D游戏.此游戏中的精灵有3个状态:idle.left和right.这看起来确实很酷!但是仅有的3个状态却限制了精灵的能力,以 ...

  6. Unity 2D游戏开发教程之2D游戏的运行效果

    Unity 2D游戏开发教程之2D游戏的运行效果 2D游戏的运行效果 本章前前后后使用了很多节的篇幅,到底实现了怎样的一个游戏运行效果呢?或者说,游戏中的精灵会不会如我们所想的那样运行呢?关于这些疑问 ...

  7. Unity 2D游戏开发教程之使用脚本实现游戏逻辑

    Unity 2D游戏开发教程之使用脚本实现游戏逻辑 使用脚本实现游戏逻辑 通过上一节的操作,我们不仅创建了精灵的动画,还设置了动画的过渡条件,最终使得精灵得以按照我们的意愿,进入我们所指定的动画状态. ...

  8. Unity 2D游戏开发教程之游戏精灵的开火状态

    Unity 2D游戏开发教程之游戏精灵的开火状态 精灵的开火状态 “开火”就是发射子弹的意思,在战争类型的电影或者电视剧中,主角们就爱这么说!本节打算为精灵添加发射子弹的能力.因为本游戏在后面会引入敌 ...

  9. Unity 2D游戏开发教程之游戏中精灵的跳跃状态

    Unity 2D游戏开发教程之游戏中精灵的跳跃状态 精灵的跳跃状态 为了让游戏中的精灵有更大的活动范围,上一节为游戏场景添加了多个地面,于是精灵可以从高的地面移动到低的地面处,如图2-14所示.但是却 ...

随机推荐

  1. C#读写ini文件操作

    ini文件,是windows操作系统下的配置文件,ini文件是一种按照特点方式排列的文本文件,它的构成分为三部分,结构如下: [Section1] key 1 = value2 key 1 = val ...

  2. 总结一下SQL的全局变量

    SQL Server 2008中的全局变量及其用法 T-SQL程序中的变量分为全局变量和局部变量两类,全局变量是由SQL Server系统定义和使用的变量.DBA和用户可以使用全局变量的值,但不能自己 ...

  3. js填写银行卡号,每隔4位数字加一个空格

    1.原生js写法 !function () { document.getElementById('bankCard').onkeyup = function (event) { var v = thi ...

  4. Vue列表渲染

    gitHub地址:https://github.com/lily1010/vue_learn/tree/master/lesson09 一 for循环数组 <!DOCTYPE html> ...

  5. FME中Cass扩展属性转Shp的方法

    问题:真受不了CAD中的注记,只能方便显示,难于数据交互.好在Cass把属性信息基本写在扩展属性中,但显示又成问题了.此事难两全!我们通过查看实体属性,需要把宗地界线的扩展属性提取出来.即组码为-3, ...

  6. This application is currently offline. To enable the application, remove the app_offline.htm file from the application root directory.

    IIS提示:This application is currently offline. To enable the application, remove the app_offline.htm f ...

  7. [linux]CentOS无法使用epel源

    [linux]CentOS无法使用epel源 问题的产生与解决 作者作为Android应用开发者,对linux的接触一直是ubuntu为主,但是有一个用于科学上网的vps,由于内存只有64M,所以使用 ...

  8. C++map类型

    map是键-值对的集合,可以理解为关联数组,可以使用键作为下标来获取一个值 本文地址:http://www.cnblogs.com/archimedes/p/cpp-map.html,转载请注明源地址 ...

  9. 在iOS 8中使用UIAlertController

    iOS 8的新特性之一就是让接口更有适应性.更灵活,因此许多视图控制器的实现方式发生了巨大的变化.全新的UIPresentationController在实现视图控制器间的过渡动画效果和自适应设备尺寸 ...

  10. Objective-C之@class的使用

    *:first-child { margin-top: 0 !important; } body > *:last-child { margin-bottom: 0 !important; } ...