unity3d开发2d游戏中控制摄像机移动以及主角移动
2014-02-27 10:24
465 查看
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游戏控制摄像机移动以及主角移动的方法
- unity3d中使用DoTween来控制2D摄像机视口的移动
- unity3d阶段性学习脚本代码(2个是摄像机跟随(2D游戏中的),1个是角色跳跃移动脚本)
- Unity3D开发之控制摄像机移动
- 【Android2D游戏开发十八】解放手指,利用传感器开发游戏!(本文讲解在SurfaceView中用重力传感器控制圆球的各方向移动)
- 【Android2D游戏开发十八】解放手指,利用传感器开发游戏!(本文讲解在SurfaceView中用重力传感器控制圆球的各方向移动)
- 游戏开发之用摇杆控制摄像机的移动旋转缩放
- Unity3D+免费工具开发2D游戏之一:工具和插件介绍
- Unity3D+免费工具开发2D游戏之三:创建人物动画
- 开发者必须mark的一款2D移动游戏开发工具
- 开发者必须mark的一款2D移动游戏开发工具
- unity3d 2D游戏中摄像机投影类型
- Android 游戏开发之主角的移动与地图的平滑滚动
- Unity3D开发之游戏兑换码的分析设计以及生成
- 【Unity3d游戏开发】浅谈Unity中的GC以及优化
- 坦克游戏教程二:实现坦克移动以及方向控制
- Unity3D 游戏开发构架篇 ——输入控制
- 游戏开发之主角的移动与地图的平滑滚动-卡马克算法
- HTML5吃豆豆游戏开发实战(二)主角移动和动画循环设置
- [Unity3D]Unity3D游戏开发之使用EasyTouch虚拟摇杆控制人物移动