捕鱼达人

鱼的移动和方向转换

usingUnityEngine;

usingSystem.Collections;

publicclassFishMove:MonoBehaviour{

publicfloatfishSpeed;

privateVector3startPos;//开始位置

privateVector3targePos;//目标位置

voidStart(){

startPos=transform.position;

GetTarget();

if(transform.position.x>0.0f){

ChangeDirection();

}

}

voidUpdate(){

if(Vector3.Distance(transform.position,targePos)<=0.05f){

ChangeDirection();

//更改起始点和目标点

startPos=targePos;

GetTarget();

}

MoveToTarget();

}

//移动

voidMoveToTarget(){

transform.position=Vector3.MoveTowards(transform.position,targePos,fishSpeed*Time.deltaTime);

}

//获取目标点

voidGetTarget(){

targePos=newVector3(startPos.x*-1,startPos.y,0);

}

voidChangeDirection(){

//1.

transform.Rotate(newVector3(0,180,0));

//2.

//Vector3scale=transform.localScale;

//scale.x=-scale.x;

//transform.localScale=scale;

}

}

用委托来监视鱼的生命值

usingUnityEngine;

usingSystem.Collections;

publicclassFishLife:MonoBehaviour{

publicGameObjectexplosionPrefabs;

publicfloathp;//鱼的生命值

publicdelegatevoidFishDelegate();

publiceventFishDelegatefishEvent;

voidStart(){

}

voidUpdate(){

}

voidOnTriggerEnter2D(Collider2Dother){

//减血

hp-=other.gameObject.GetComponent().attack;

//子弹消失

if(other.name.Contains("fire")){

Destroy(other.gameObject);

}

if(hp<=0){

//产生爆炸效果

GameObjectexplosion=Instantiate(explosionPrefabs,transform.position,Quaternion.identity)asGameObject;

fishEvent();

Destroy(gameObject);

}

}

}



usingUnityEngine;

usingSystem.Collections;

publicclassGameController01:MonoBehaviour{

privatestring[]fishName;

privatefloattime;

privateintkillNumber;

privatefloattime_1;

voidStart(){

fishName=newstring[]{"fish1","fish2","fish3"};

}

voidUpdate(){

time+=Time.deltaTime;

if(time>=1.5f){

CreatFish();

time=0.0f;

}

}

voidCreatFish(){

intfishIndex=Random.Range(0,3);

//从资源文件夹Resources获取预设体

GameObjectfishPrefabs=Resources.Load(fishName[fishIndex])asGameObject;

intx_Dic=Random.Range(0,2);

floaty=Random.Range(-2,5.84f);

Vector3fishCreatPos;

if(x_Dic==0){

fishCreatPos=newVector3(-13.66f,y,0);

}else{

fishCreatPos=newVector3(13.66f,y,0);

}

GameObjectfish=Instantiate(fishPrefabs,fishCreatPos,Quaternion.identity)asGameObject;

fish.GetComponent().fishEvent+=ShowDate;

}

voidShowDate(){

killNumber++;

print(killNumber);

if((Time.time-time_1)<=1.0f){

}

time_1=Time.time;

}

}




用鼠标控制炮台的旋转

usingUnityEngine;

usingSystem.Collections;

publicclassCannonScript:MonoBehaviour{

publicGameObjectbulletPrefabs;//子弹预设体

publicTransformshootPos;//获得子弹发射位置

voidStart(){

}

voidUpdate(){

FollowMouseRotation();

if(Input.GetMouseButtonDown(0)){

Shoot();

}

}

voidShoot(){

GameObjectbullet=Instantiate(bulletPrefabs,shootPos.position,transform.rotation)asGameObject;

}

voidFollowMouseRotation(){

Vector3mousePos=Camera.main.ScreenToWorldPoint(Input.mousePosition);

Vector3direction=mousePos-transform.position;

direction.z=0;

//限制旋转角度

if(direction.y>0.2f){

//让自身坐标的Y轴指向当前方向

transform.up=direction;

}

}

}



usingUnityEngine;

usingSystem.Collections;

publicclassbulletScript:MonoBehaviour{

publicfloatattack;

privateRigidbody2Drig2D;

voidStart(){

rig2D=GetComponent();

}

voidUpdate(){

rig2D.AddForce(transform.up*1,ForceMode2D.Impulse);

}

}



usingUnityEngine;

usingSystem.Collections;

publicclassexplosionScript:MonoBehaviour{

//Usethisforinitialization

voidStart(){

}

//Updateiscalledonceperframe

voidUpdate(){

}

voidDead(){

Destroy(gameObject);

}

}

你可能感兴趣的:(捕鱼达人)