unity5.0入门案例《unity5.0太空射击》 扫二维码继续学习 二维码时效为半小时

(28 评论)

免费

添加mover代码

public float speed;

 

 

start ()

GetCoomponent<Rigidbody>().velocity = transform.forward * speed;

 

 

勾上convex

 

创建

 

public GameObject shot;

public Transform shotSpwan;

public float fireRate; 

 

private float nextFire;

update 

if (Input.GetButton ("Fire1")&&Time.time>nextFire){

nextFure = Time.time + fireRate;

Instantiate (shot, shotSpawn.position, shotSpawn.ratation);

}

 

[展开全文]

消除子弹的方法:

在子弹对象脚本中添加位置检测

if (transform.position.z > 14.0f)
 Destroy (this.gameObject);

[展开全文]

二维平面+贴图制作子弹

给平面加刚体加碰撞属性

把平面放到空对象里

 

[展开全文]
using UnityEngine; using System.Collections; public class player : MonoBehaviour { public float speed; // Use this for initialization void Start () { } // Update is called once per frame void Update () { } void FixedUPdate(){ float moveHorizontal = Input.GetAxis ("Horizontal"); float movevertical = Input.GetAxis ("Vertical"); Vector3 movement = new Vector3 (moveHorizontal, 0.0f, movevertical); //rigidbody GetComponent ().velocity = movement * speed; }
[展开全文]

当前子弹的设置能够连着穿透两个小行星,倒数第三讲有相应的修改

[展开全文]

当前发出子弹能够

[展开全文]

例子中爆炸效果为销毁,可以用Destroy

Destroy销毁gameobject,也可以设置隔多少秒

后消除

Destroy(Instantiate(explosion, this.transform.position, this.transform.rotation), 2f);   //在小行星销毁的地方创建爆炸

[展开全文]

哭死了,按着教程走,前面FixedUpdate设置的速度z是currentSpeed,结果在28分钟10多秒的时候直接秒改成-5,造成了我运行敌机没z轴速度,教程可以下来。。。。

[展开全文]

class GameController()

{

public bool gameOver;

public bool restart;

void Start(){

gameOver=false;

restart=false;

}

public void GameOver(){

gameOverText="Game Over";

gameOver=true;

}

IEumerator SpawnWave(){

if(gameOver){

restartText="Press 'R' For Restart";

restart=true;

break;

}

}

void Update(){

if(restart){

if(Input.GetKeyDown(KeyCode.R)){

Application.LoadLevel(Application.loadLevel);

}

}

}

}

 

public class DestroyByContact(){

void OnTriggerEnter(){

gameController.GameOver();

}

[展开全文]

class GameController{

using UnityEngine.UI;

public float scoreText;

public float gameOverText;

public float restartText;

private int score;

void start()

{

gameOverText.text="";

restartText.text="";

scoreTxet=0;

UpdateScore();

 

}

public void AddScore(int newScoreValues){

score+=newScoreValues;

UpdateScore();

 

}

void UpdateScore(){

scoreTxet.text="score"+score;

}

}

class DestroyByCantact(){

public GameController gameController;

public int scoreValue;

void start(){

GameObject gameControllerObject=GameObject.FindWithTag("GameController");

if(gameControllerObject!=null){

gameController=gameControllerObjct.GetCoponent<GameController>();

}

if(gameController==null){

Debug.Log("Cannot find 'GameController' Script");

}

}

void OnTriggerEnter(){

gameController.AddScore(scoreValue);

}

}

[展开全文]

敌机机动

public class EvasiveManeuver :MonoBehevior

{

public Boundary boundary;//将Boundary class 提取出来

public float tilt ;//倾斜

public float dodge;//躲闪

public float smoothing;

public Vector2 startWait;//开始机动时候的等待时间

public Vector2 maneuverTime;//机动时间 

public Vector2 maneuverWait;//机动后再次机动的等待时间

private float currentSpeed;//当前敌人的速度

privete float targerManeuver;//机动的目标值

void Start(){

currentSpeed=GetComponent<Rigidbody>().velocity.z;

startCoroutine(Evade());

}

IEnumerator Evade(){

yield return new WaitForSeconds(Random.Range(startWait.x,startWait.y));

while(ture){

targetManeuver=Random.Range(1,dadge)*-Mathf.Sign(transform.position.x);

yield return new WaitForSeconds(Random.Range(maneuverTime.x,maneuverTime.y));

targetManeuver=0;

yield return new WaitForSeconds(Random.Range(maneuverWait.x,maneuverwWait.y));

}

void FixedUpdate(){

float new Maneuver=Mathf.MoveTowards(GetComponent<Rigidbody>().velocity.x,targetManenuver,smoothing*Time.deltaTime);

GetComponent<Rigidbody>().velocity=new Vector3(newManeuver,0.0f,currentSpeed);

GetComponent<Rigidbody>().position=new Vector3(Mathf.Clamp(GetComponent<Rigidbody>().position.x,boundary.xMin,boundary.xMax)),0.0f,Mathf.Clamp(GetComponent<Rigidbody>().z,boundary.zMin,zMax);

GetComponent<Rigidbody>().rotation=Quaternion.Euler(0,0,GetComponent<Rigidbody>().velocity.x*tilt)

}

}

}

 

[展开全文]