在unity3d开发2d游戏控制摄像机移动以及主角移动的方法
2014-02-27 10:27
567 查看
unity3d开发2d游戏中有了摄像机照射的区域以及背景地图的宽高尺寸那么就可以在代码中编写逻辑判断条件啦。下面我们来使用简单的代码控制摄像机移动以及主角移动。
01using UnityEngine;
02using System.Collections;
03
04public class Controller : MonoBehaviour
05{
06
07 //动画数组
08 private Object[] anim;
09 //主角对象
10 private GameObject hero;
11 //限制一秒多少帧
12 private float fps = 10;
13 //帧序列
14 private int nowFram;
15 //记录当前时间
16 private float time;
17
18 void Start ()
19 {
20 //得到资源名称为down文件夹中的所有对象资源
21 anim = Resources.LoadAll("down");
22 //得到主角的对象
23 hero = GameObject.Find("hero");
24 }
25
26 void FixedUpdate ()
27 {
28 //上、下、左、右平移摄像机
29 if (Input.GetKey (KeyCode.A))
30 {
31 transform.Translate(-0.01f,0,0);
32
33 }
34
35 if(Input.GetKey (KeyCode.D))
36 {
37 transform.Translate(0.01f,0,0);
38 }
39
40 if (Input.GetKey (KeyCode.W))
41 {
42 transform.Translate(0,0.01f,0);
43 }
44
45 if(Input.GetKey (KeyCode.S))
46 {
47 transform.Translate(0,-0.01f,0);
48 }
49
50 //上、下、左、右平移主角
51 if (Input.GetKey (KeyCode.J))
52 {
53
54 hero.transform.Translate(0.001f,0,0);
55 }
56
57 if(Input.GetKey (KeyCode.L))
58 {
59
60 hero.transform.Translate(-0.001f,0,0);
61 }
62
63 if (Input.GetKey (KeyCode.I))
64 {
65 hero.transform.Translate(0,0,-0.001f);
66 }
67
68 if(Input.GetKey (KeyCode.K))
69 {
70 hero.transform.Translate(0,0,0.001f);
71 }
72
73 DrawAnimation(anim);
74 }
75
76 void DrawAnimation(Object[] tex)
77 {
78
79 //计算限制帧的时间
80 time += Time.deltaTime;
81 //超过限制帧切换贴图
82 if(time >= 1.0 / fps){
83 //帧序列切换
84 nowFram++;
85 //限制帧清空
86 time = 0;
87 //超过帧动画总数从第0帧开始
88 if(nowFram >= tex.Length)
89 {
90 nowFram = 0;
91 }
92 }
93 //将对应的贴图赋予主角对象,强制将资源文件转换成贴图
94 hero.renderer.material.mainTexture = (Texture)tex[nowFram];
95 }
96}
01using UnityEngine;
02using System.Collections;
03
04public class Controller : MonoBehaviour
05{
06
07 //动画数组
08 private Object[] anim;
09 //主角对象
10 private GameObject hero;
11 //限制一秒多少帧
12 private float fps = 10;
13 //帧序列
14 private int nowFram;
15 //记录当前时间
16 private float time;
17
18 void Start ()
19 {
20 //得到资源名称为down文件夹中的所有对象资源
21 anim = Resources.LoadAll("down");
22 //得到主角的对象
23 hero = GameObject.Find("hero");
24 }
25
26 void FixedUpdate ()
27 {
28 //上、下、左、右平移摄像机
29 if (Input.GetKey (KeyCode.A))
30 {
31 transform.Translate(-0.01f,0,0);
32
33 }
34
35 if(Input.GetKey (KeyCode.D))
36 {
37 transform.Translate(0.01f,0,0);
38 }
39
40 if (Input.GetKey (KeyCode.W))
41 {
42 transform.Translate(0,0.01f,0);
43 }
44
45 if(Input.GetKey (KeyCode.S))
46 {
47 transform.Translate(0,-0.01f,0);
48 }
49
50 //上、下、左、右平移主角
51 if (Input.GetKey (KeyCode.J))
52 {
53
54 hero.transform.Translate(0.001f,0,0);
55 }
56
57 if(Input.GetKey (KeyCode.L))
58 {
59
60 hero.transform.Translate(-0.001f,0,0);
61 }
62
63 if (Input.GetKey (KeyCode.I))
64 {
65 hero.transform.Translate(0,0,-0.001f);
66 }
67
68 if(Input.GetKey (KeyCode.K))
69 {
70 hero.transform.Translate(0,0,0.001f);
71 }
72
73 DrawAnimation(anim);
74 }
75
76 void DrawAnimation(Object[] tex)
77 {
78
79 //计算限制帧的时间
80 time += Time.deltaTime;
81 //超过限制帧切换贴图
82 if(time >= 1.0 / fps){
83 //帧序列切换
84 nowFram++;
85 //限制帧清空
86 time = 0;
87 //超过帧动画总数从第0帧开始
88 if(nowFram >= tex.Length)
89 {
90 nowFram = 0;
91 }
92 }
93 //将对应的贴图赋予主角对象,强制将资源文件转换成贴图
94 hero.renderer.material.mainTexture = (Texture)tex[nowFram];
95 }
96}
相关文章推荐
- unity3d开发2d游戏中控制摄像机移动以及主角移动
- 【Android2D游戏开发十八】解放手指,利用传感器开发游戏!(本文讲解在SurfaceView中用重力传感器控制圆球的各方向移动)
- 游戏开发之用摇杆控制摄像机的移动旋转缩放
- Unity3D开发之控制摄像机移动
- unity3d阶段性学习脚本代码(2个是摄像机跟随(2D游戏中的),1个是角色跳跃移动脚本)
- unity3d中使用DoTween来控制2D摄像机视口的移动
- unity3D实际的原始视频游戏开发系列讲座12之U3D的2D为了开发实战的新方法
- 【Android2D游戏开发十八】解放手指,利用传感器开发游戏!(本文讲解在SurfaceView中用重力传感器控制圆球的各方向移动)
- U3d学习-使用Unity3D开发2D游戏(上)
- 开发者必须mark的一款2D移动游戏开发工具
- Unity3D 游戏开发构架篇 ——输入控制
- Unity3D游戏开发之使用Unity3D开发2D游戏(二)(2DTookit插件亲测)
- Unity3D游戏场景之控制模型移动、旋转和相机跟随
- 工具设置Unity3D系列教程--使用免费工具在Unity3D中开发2D游戏 第一节
- Unity3D系列教程–使用免费工具在Unity3D中开发2D游戏 第二节(上)
- android 2D 游戏的开发的方法
- Unity3D研究院之2D游戏开发制作原理(二十一)
- [Unity3D]手机3D游戏开发:简单的移动和缩放鼠标手势脚本源码
- JAVAWEB开发之Servlet3.0新特性的使用以及注解的详细使用和自定义注解的方法、动态代理的使用、利用动态代理实现细粒度的权限控制以及类加载和泛型反射