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. 关于SQL Server的WITH(NOLOCK)和(NOLOCK)

    The difference is that you should be using the syntax WITH (NOLOCK) (or WITH (<any table hint> ...

  2. ServiceLocator是反模式

    关于ServiceLocator模式 http://www.cnblogs.com/hwade/archive/2011/01/30/CommonServiceLocator.html 为什么是Ant ...

  3. swift学习笔记之-闭包

    //闭包 import UIKit /*闭包(Closures): 函数.闭包.类都是引用类型(引用类型的实例赋值给变量或常量时,得到的都是该实例的引用,而值类型的实例变量得到的是独立的值的拷贝) 1 ...

  4. 中国象棋引擎的C#源代码

    以前写的中国象棋引擎的C#源程序,可在VS2010中编译运行,由于个人精力有限,难以完成后续的开发工作,如果谁感兴趣,请关注微信公众号(“申龙斌的程序人生”,ID:slbGTD),发送后台消息“象棋引 ...

  5. Spring中配置数据源的4种形式(转)

    原文http://blog.csdn.net/orclight/article/details/8616103       不管采用何种持久化技术,都需要定义数据源.Spring中提供了4种不同形式的 ...

  6. C语言-07-预处理、typedef、static和extern

    预处理 1> 使用注意 以#开头 在代码被翻译成0和1之前执行 预处理指令可以出现在任何位置 作用域是从编写指令那一行开始到文件结束 2> 宏定义 基本使用 ① 使用#define定义 ② ...

  7. 异常处理——毕向东Java基础教程学习笔记

    1.异常:就是程序运行过程中出现的不正常情况. 异常的由来:问题本身也是日常生活中一个具体的事物,也可以通过java类的形式进行描述,并封装成对象.                        其实 ...

  8. linux crontab & 每隔10秒执行一次

    linux下定时执行任务的方法  在LINUX中你应该先输入crontab -e,然后就会有个vi编辑界面,再输入0 3 * * 1 /clearigame2内容到里面 :wq 保存退出. 在LINU ...

  9. Effective Java 21 Use function objects to represent strategies

    Theory In the Java the function pointers is implemented by the declaring an interface to represent s ...

  10. 在Web api2 中传递复杂参数的一点心得

    这两天在做的一个项目基于webapi2,期间遇到了复杂参数传递的问题.其中刚好看到园友的这篇文章,但是我测试收结果是失败的,还不知道是什么原因.最终经过思考后,找到了一种方法,和大家分享下. 在前端我 ...