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. ASP.NET Web API 通过Authentication特性来实现身份认证

    using System; using System.Collections.Generic; using System.Net.Http.Headers; using System.Security ...

  2. .NET Framework 4.0之Tuple(元组)

    Tuple,是函数式编程的概念之一,早见于Elang.F#等动态语言.Tuple类型像一个口袋,在出门前可以把所需的任何东西一股脑地放在里面.您可以将钥匙.驾驶证.便笺簿和钢笔放在口袋里,您的口袋是存 ...

  3. Java内存泄露的原因

    Java内存泄露的原因 1.静态集合类像HashMap.Vector等的使用最容易出现内存泄露,这些静态变量的生命周期和应用程序一致,所有的对象Object也不能被释放,因为他们也将一直被Vector ...

  4. SQL数据库基础(三)

    认识数据库备份和事务日志备份 数据库备份与日志备份是数据库维护的日常工作,备份的目的是在于当数据库出现故障或者遭到破坏时可以根据备份的数据库及事务日志文件还原到最近的时间点将损失降到最低点. 数据库备 ...

  5. (三)play之yabe项目【数据模型】

    (三)play之yabe项目[数据模型] 博客分类: 框架@play framework   创建项目 play new yabe What is the application name? [yab ...

  6. javascript --- 设计模式之单体模式(一)

    单体是一个用来划分命名空间并将一些相关的属性与方法组织在一起的对象,如果她可以被实例化的话,那她只能被实例化一次(她只能嫁一次,不能二婚). 单体模式是javascript里面最基本但也是最有用的模式 ...

  7. C#源码500份

    C Sharp  短信发送平台源代码.rar http://1000eb.com/5c6vASP.NET+AJAX基础示例 视频教程 http://1000eb.com/89jcC# Winform ...

  8. ArcGIS补丁包下载

    http://zhihu.esrichina.com.cn/?/feature/patchdownload

  9. 转:【前端福利】用grunt搭建自动化的web前端开发环境-完整教程

    原文地址:http://blog.csdn.net/wangfupeng1988/article/details/46418203 jQuery在使用grunt,bootstrap在使用grunt,百 ...

  10. R语言学习笔记:简单的回归分析

    fitbit <- read.csv("fitbit.csv") date     cal   step  dist floor sit inactive walk run2 ...