Unity學習筆記 之 發射小球碰撞物體的代碼記錄


綁定在攝像機上的腳本

using UnityEngine;
using System.Collections;

public class abc : MonoBehaviour {

	//設置移動速度
	public int speed = 5;

	//設置將被初始化載入的對象
	public Transform newobject = null;

	// Use this for initialization
	void Start () {
	
	}
	
	// Update is called once per frame
	void Update () {
		//通過左右方向鍵,或A、D字母鍵控制水平方向。實現往左、往右移動
		float x = Input.GetAxis("Horizontal") * Time.deltaTime * speed;
		//通過上下方向鍵,或W、S字母鍵控制垂直方向,實現往前、往后移動
		float z = Input.GetAxis("Vertical") * Time.deltaTime * speed;
		//移動 綁定物的 x、z 軸,即移動 攝像機的 x、z 軸。

transform.Translate(x,0,z); //推斷是否按下鼠標的左鍵 if (Input.GetButtonDown("Fire1")) { //實例化命令:Instantiate(要生成的物體, 生成的位置, 生成物體的旋轉角度) Transform n = (Transform)Instantiate(newobject, transform.position, transform.rotation); //轉換方向 Vector3 fwd = transform.TransformDirection(Vector3.forward); //給物體加入力度 //Unity5之前的寫法:n.rigidbody.AddForce(fwd * 2800); n.GetComponent<Rigidbody>().AddForce(fwd * 2800); } //推斷是否按下字母button Q if (Input.GetKey(KeyCode.Q)) { //改變 綁定物的 y 軸,即改變 攝像機的 y 軸。 transform.Rotate(0,-25*Time.deltaTime,0,Space.Self); } //推斷是否按下字母button E if (Input.GetKey(KeyCode.E)) { transform.Rotate(0,25*Time.deltaTime,0,Space.Self); } //推斷是否按下字母button Z if (Input.GetKey(KeyCode.Z)) { //旋轉 綁定物的 y 軸,即旋轉 攝像機的 y 軸。 transform.Rotate(-25*Time.deltaTime,0,0,Space.Self); } //推斷是否按下字母button X if (Input.GetKey(KeyCode.X)) { //旋轉 綁定物的 y 軸,即旋轉 攝像機的 y 軸。 transform.Rotate(25*Time.deltaTime,0,0,Space.Self); } //推斷是否按下字母button F if (Input.GetKey(KeyCode.F)) { //移動 綁定物的 y 軸。即移動 攝像機的 y 軸。 transform.Translate(0,-5*Time.deltaTime,0); } //推斷是否按下字母button C if (Input.GetKey(KeyCode.C)) { //移動 綁定物的 y 軸,即移動 攝像機的 y 軸。

transform.Translate(0,5*Time.deltaTime,0); } } }



綁定在發射的小球上的腳本

using UnityEngine;
using System.Collections;

public class xiaomie : MonoBehaviour {

	// Use this for initialization
	void Start () {
		//銷毀物體,gameObject。目測應該是指物體自身。即達到自我銷毀的需求.
		Destroy(gameObject, 3.0f);
	}
	
	// Update is called once per frame
	void Update () {
	
	}
}



免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM