码迷,mamicode.com
首页 > 编程语言 > 详细

unity学习笔记1012(platform)

时间:2014-10-15 23:15:21      阅读:429      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   color   io   os   ar   for   strong   

前传:

今天,佳伟师兄给我们讲了第三个案例,很遗憾,第一个第二个我没能想起用博客园写,博客园自从大一暑假申请之后就再也没用过,但是真的会觉得遗憾。以后有时间,我还是会把算法一点点的攻破~这是我作为一个计算机专业学生的一个必须要做的事情。

主题:unity4.3游戏开发项目实战 第三例 platform(游戏平台)

首先说明,佳伟师兄讲的真的挺好的,无论是代码还是具体的操作,讲的很清楚。

一 游戏的策划我就不详细写了,我觉得我现在还没到能够写出策划的地步,下面我具体详写各个功能模块实现的功能。

二,模块功能:

1,level1的实现与功能分析

在做这个的过程中,我体会到的是各个场景中物体的位置的具体设置,数据可以按照自己的需求来设置。需要设置的是游戏的背景,平台,钻石,人物。

下面来说数据。你需要做的第一步是在场景中加入一个主照相机,照相机上是纵观全局的,上面设置有全局的声音,还有平行光源,其次记得要把照相机设置成2D形式,即Orthographic格式。书本上说把照相机的位置设置成z=-5,近距离可以看到-0.3,最远看到20,我们把最远设置成10就ok.(各种导入资源的我就不说了,基础了,记得在最开始的时候,保存好场景。保存好工程。)

背景的位置(0,0,9)

平台的位置(0,0,5)

照相机的位置(0,0,-1)

 

为了保证所发生的物体在同一个界面以发生触发,所以所有的物体必须保证在同一界面上即z=0

关于物体的世界坐标与局部坐标,我决定好好看一下,我感觉这是我的软肋。

恩恩// 问别人了 相对于一个处于世界坐标系的父物体而言,子物体在世界坐标系的位置等于子物体的坐标加上父物体的坐标//

纵观全局,我们需要给钻石,平台添加碰撞器,钻石需要加触发,以便在人物收取钻石时,产生销毁的效果,人物要加人物控制器

调节好每个物体的位置之后,我们开始写各个角色的代码

按照书本上的顺序来,我先把gem即钻石的代码整理好。gem有以下几个功能要实现:实现在一定距离范围内上下在平台上移动,其次是在人物碰撞时销毁

我们在倒入资源的时候,首先给钻石找到一个合适的位置,即定义钻石的初始位置,这个放在start函数内,毋庸置疑 startposition=transform.position.y因为钻石在上下移动,跟X轴无关。

初始化定义的钻石的速度,起始位置,和一个是否向下的bool变量

private float speed=3.0f;

private flaot startPosition;

private bool isDown=true;

接着在upstate里面写入钻石的运动模式

声明在一定的

if(isDown)

transform.Translate(speed*Time.deltaTime*Vector3.down);

else

transform.Tranlate(Vector3.up*speed*Time.deltaTime);

判断钻石的运动方向

if(startPosition-transform.positon>0.4f)

isDown=false;

if(transform.position-startPosition>0.4f)

isDown=true;

在给人物贴好标签之后,人物碰到钻石之后会销毁

void OnTriggerEnter(Collider other)

{

if(other.tag=="player")

{

Audio.Source.PlayClipAtPoint(SoundName,new Vector3(0,0,-10);//http://www.unitymanual.com/home.php?do=blog&id=141&mod=space&uid=12062详细的看这个吧,在指定位置播放剪辑。播放完成后自动消除音频源。

Detroy(gameObject);

}

}

下面来看看人物的运动  这是个大头

首先说说 人物帧的变化

先来定义

public int frameNumber=10;//书上说刚开始把人物run的帧的图片拖入到屏幕上,上次看见一个学弟说怎么自己弄一张贴图代码加入进去之后不会动,是因为资料里面的图片是美工做的一系列的动画,并不是仅仅一张贴图~~

public bool direction=true;判断人物最开始的run方向

public int lastframeNo=0;

private bool oneTime=true;这个是定义是否是一次运动过程

private int index=0;

private float frameRate=0;

private float myTime=0;定义初始值我的时间为0

private int myIndex=0;//这个是在行动的过程中帧的变化

接下来先写左右运动的判断

if(direction)

{

render.material.mainTextureScale=new Vector2(frameRate,1);//是贴图Uv的平铺坐标

render.material.mainTextureOffset=new Vector2(index*frameRate,0);//render.material.mainTextureOffset是贴图的偏移量

}

else

render.material.mainTextureScale=new Vecor2(-frameRate,1);

render.material.mainTextureOffset=new Vector2(index*frameRate,0);

再来看一下其余的逻辑

void update()

{

fameRate=1.0f/frameNumber;//1.0f/frameNumber  1/小图个数=每一个小图的长站大图的比例   

if(oneTime)//一次开关

{

myTime+=Time.deltaTime;//时间的累加

myIndex=(int)(myTime*(frameNumber-1))//运动过程中帧的变化

index=myIndex%frameNumber;

}

if(lastFrameNo!=0)

{

if(index==lastFrameNo-1)//帧是一个数组,从0开始的,第几帧==frameNumber-1

oneTime=false;

}

}

接下来,研究一下人物游戏状态~//送给自己一句话,艰难的坚持回比轻易的放弃换回来的多很多!!

 人物分为以下几种状态,采用枚举类型实现列举

public enum GameState

{

idle,

runLeft,

runRight,

jumpRight,

jumpLeft,

idleLeftJump,

idleRightJump

celebrate,

die

}

PlayerStateController的代码实现如下

在初始定义时,运动状态的设置注意不要遗漏,我自己默写试试

public Texture idle;

public Texture run;

public Texture jump;

public Texture celebrate;

publicTexture  die;

public  x winTexture;

public Texture2D dieTexture;

public GameState gamestate;

声音

public AudioClip soundName;

public AudioClip dieSound;

在设置几个开关

private bool moveDirection=true;

private bool exit=false;

private bool youWin=false;

private bool youDie=false;

private AnimationController myAnimation;

在Start里面获取AnimationController的组件,便于对人物的状态进行更改和利yong

void start()

{

myAnimation=GetComponent<AnimationController>();

}

在update()里面继续实现人物的动作

首先实现的是实现人物九个基本动作的逻辑

if(Input.GetAxis("Horizontal"))

{

moveDirection=false;

if(Input.GetButton("Jump"))

gameState=GameState.jumpRight;

else

gameState=GameState.runRight;

}

else if(Input.GetAxis("Horizontal")<0)

{

moveDirection=true;

if(Input.GetButton(""Jump))

gameState=GameState.jumpLeft;

else

gameState=GameState.runLeft;

}

else

if(Input.GtButton("Jump"))

{

if(moveDirection)

gameState=GameState.idleLeftJump;

else

gameState=GameState.idleRightJump;

}

else gameState=GameState.idle;

}

if(youwin)

gameState=GameState.celebarate;

if(youDie)

gameState=GameState.die;

代码逻辑易懂,但是不要遗漏人物的动作

接下来,要用switch语句写,以一个例子为例

switch(gamestate)

{

case GameState.idle:

transform.render.material.SetTexture("_MainTex",idle);

myAnimation.frameNumber=1;

myAnimation.direction=false;

break;

}

在 通过第一关的时候

if(Input.GetButton("jump")&&youWin)

{

if(Application.loadedLeveiName=="Level1")

Application.LoadLevel("level2");

else if(Application.LoadedLevelName=="level2")

Application.LoadLevel("level3");

else 

Application.LoadLevel("level1");

}

 if(Input.GetButton("Jump")&&youDie)

{

transform.position=new Vector3(0,-2,0);

transform.collider.enabled=true;

youDie=false;

}

IEnumberator OnTriggerEnter(Collider other)

{

if(other.tag=="exit"&&GameObject.FindWithTag("gem")==null)

{

youWin=true;

AudioSource.PlayClipAtPoint(SoundName,new Vector3(0,0,-10));

}

if(other.tag=="enemy")

{

youDie=true;

AudioSource.PlayClipAtPoint(dieSound,newVector3(0,0,-10));位置决定声音的大小

yied return new WaitForSeconds(0.5f);屏幕缓冲5秒钟

transform.collider.enabled=false;

}

 GUI界面的设置

if(youWin)

{

GUI.DrawTexture(new Rect(Screen.width/2-1/9,Screen.height/2-90,358,180),winTexture,ScaleMode.ScaleToFit,true,0);

}

PlayController  方面

public class PlayerController : MonoBehaviour
{
public float speed = 2.0f;
private CharacterController controller;
private Vector3 velocity = Vector3.zero;
private float gravity = 20.0f;
private float jumpSpeed = 4.5f;
private GameState gameState;
private PlayerController myPlayerStateController;

// Use this for initialization
void Start ()
{
controller = GetComponent<CharacterController>();
myPlayerStateController = GetComponent<PlayerController>();

}

// Update is called once per frame
void Update ()
{
gameState = myPlayerStateController.gameState;
if (controller.isGrounded)
{
switch (gameState)
{
case GameState.runRight:
velocity = speed * (new Vector3(1, 0, 0));
break;
case GameState.runLeft:
velocity = speed * (new Vector3(-1, 0, 0));
break;
case GameState.jumpRight :
velocity = speed * new Vector3(-1, jumpSpeed, 0);
break;
case GameState .jumpLeft:
velocity = speed * new Vector3(-1, 0, 0);
break;
case GameState .idleLeftJump:
velocity = speed * new Vector3(0, jumpSpeed, 0);
break;
case GameState .idleRightJump:
velocity = speed * new Vector3(0, jumpSpeed, 0);
break;
case GameState .celebrate :
velocity = Vector3.zero;
break;

default:
velocity = Vector3.zero;
break;

 

}
}
else
{
velocity.y -= gravity * Time.deltaTime;
}
controller.Move(velocity * Time.deltaTime);
}
}

 

   

 

unity学习笔记1012(platform)

标签:style   blog   http   color   io   os   ar   for   strong   

原文地址:http://www.cnblogs.com/030lixia/p/4021156.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!