【SIKIA计划】_06_Unity2D游戏开发-拾荒者笔记

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/jvao_q7/article/details/84580046

【新增分类】

Animations 动画

——Animation

——AnimatorController

Prefabs 预制

【素材导入】

unitypackage 素材包

Sprites Editor 编辑图片(切图之类)

【素材应用】

1.选取一个动作多张图片拖动到Hierarchy生成动画

2.带有Sprite Renderer和Animator组件

3.在project会生成anim(动画)和controller(控制)文件

4.选取另一个动作多张图片拖拽到刚生成的物体则不会创建新物体

5.把物体拖拽到project生成预制体

(同一控制不同动画处理)

6.右键创建Animator Overide Controller重写一个相同的控制器,指定重写对象

7.把2动画借1物体生成动画,并改1物体指定控制和重写的状态机指定动画。

【动画编辑】【动画状态机】

Animator 动画编辑器 状态机

idle 待命

attack 攻击

damger 受伤

Trigger

——Has Exit Time 前一个动画播放完触发

——Exit Time 多久进行切换

——Transition Duration 转化时间(3D需要融合时间)

[动画组件]

private Animator animator;

animator = GetComponent<Animator>();

animator.SetTrigger("Attack");

【固定生成】【设置父类】

//创建一个空物体,创建脚本

GameObject[] outWallArray;

//指定预设物体

private Transform mapHolder;(1)

//地址

public int rows=10;

public int cols=10;

private void InitMap(){

//初始化地图,

mapHolder = new GameObject("Map").transform;(2)

for(int x=0;x<cols;x++){

for(int y =0; y <rows;y++){

if(x==0||y==0||x==cols-1||y==rows-1){

int index = Random.Range(0,outWallArray,length)

GameObject go = GameObject.Instantiate(outWallArray[index],new vector3(x,y,0),Quaternion.identity)as GameObject;

//对象,位置,旋转

go.transform.SetParent(mapHolder):(3)

//设置父类

}

}

}

}

【随机生成】【List的使用】

public GameObject[] wallArray;

public int minCountWall = 2;

public int maiCountWall = 8;

private List<Vector2> polistionList = new List<Vector2>();

positionList.Clear();

for(int x = 2; x < cols-2;x++){

for(int y = 2; y < cols-2;y++){

positionList.Add(new Vector2(x,y))

}

}

//障碍物的地址

int wallCount = Random.Range(minCountWall, maxCountWall + 1);、

//障碍物的个数

for(int i = 0;i <wallCount;i++){

int positionIndex = Random.Rabge(0,positionList.Count)

Vector2 pos = positionList[positionIndex];

positionList.Remove(positionIndex);

//随机取得位置,移出列表

int wallIndex = Random.Range(0,wallArray.length);

//随机取得障碍物

GameObject go = GameObject.Instantiate(wallArray[wallIndex],pos,Quaternion.identity) as GameObject ;

go.transform.SetParent(mapHolder):

}

注意:记得设置墙体和障碍物层数

【互相调用】

同一物体两个组件

{GameManager}

public int level

{MapManager}

private GameManager gameManager;

gameManager = this.GetComponent<GameManager>();

不同物体两个组件

{GameManager}

public List<Enemy> enemyList = new List<Enemy>();

{Enemy}

GameManager.Instance.enemyList.Add(this);

【预设输入】

float h = Input.GetAxisRaw("Horizontal");

float h = Input.GetAxisRaw("Vertical");

if(h>0){

v = 0;

}

【刚体组件】

rigidbody.MovePosition(Vector2.Lerp(transform.position,targetPos.smoothing*Time.deltaTime));

//差值(初始位置,目标位置,速度)

【计时休息】

public float restTimer = 1;//限时多少

private float restTime = 0;//计时器

restTimer += Time.deltaTime;

if(restTimer<restTime)retrun;

//运行

restTimer = 0;

【目标检测】

colider.enabled = false; //自身collider禁用

RaycastHit2D hit = Physics2D.Linecast(targetPos,targetPos + new Vector2(h,v));

//初始位置,目标位置

collider.enabled = true;

if(hit.transform == null){

targetPos += new Vector2(h,v);

}else{

switch(hit.collider.tag) {

case"OutWall"

break;

case"Wall"

hit.collider.SendMessage("TakeDamage")

}

}

restTimer = 0;

//碰撞到物体标签

【物体查找】

private Transform player

player = GameObject.FindGameObjectWithTag("player").transform;

【AI判断】

Vector2 offset = player.position - transform.position;

if(offset.magnitude<1.1f){

//攻击

}else{

float x = 0, y = 0;

if(Mathf.Abs(offset.y)>Mathf.Abs(offset.x)){

//按照Y轴移动,绝对值

if(offset.y<0){

y = -1;

}else

{

y = 1;

}

}else{

//按照x轴移动

if(offset.x > 0){

x = 1;

}else{

x = -1;

}

}

tagetPosition +=new Vector2(x,y);

}

【开启禁用】

this.enabled = false;//禁用该脚本

if(GameManager.Instance.food)

private Text foodText;

foodText = GameObject.Find("FailText").GetComponent<Text>();

failText.enabled = false;

failText.enabled = true

[HideInspector]public Vector2 targetPos = new Vector2(1,1);

DontDestroyOnload(gameObject);//不要销毁

void HideBlack(){

dayImage.gameObject.SetActive(false);

}

Invoke("HideBlack",1)//经过一秒调用

【关卡变化】

Application.LoadLevel(Application.loadedLevel);//重新加载本关卡

void OnLevelWasLoaded (int sceneLevel){

Level++;

InitGame();//初始化游戏

}

【实例化】

GameManager放在预设里

在Main Camer添加一个新脚本Leader

public GameObjiect gameManager;

void Awake(){

if(GameManager.Instance==null)//等于空才进行实例化

GameObject.Instantiate(gameManager);

}

【音效播放】

public float minPitch = 0.9f;

public float manPitch = 1.1f;

public AudioSource efxSource;

public void RandomPlay(params AudioClip[] clips){

float pitch = Random.Range(minPitch,manPitch);

int index = Random.Range(0,clips.Length);

AudioClip clip = clips[index];

efxSource.clip =clip;

efxSource.pitch = pitch;

efxSource.Play();

}

猜你喜欢

转载自blog.csdn.net/jvao_q7/article/details/84580046
今日推荐