目前分類:unity C# (26)
- Oct 15 Thu 2015 02:41
Unity Draw Software - Update.....
- Jul 03 Fri 2015 04:20
Unity 小畫家
- Jun 15 Mon 2015 05:10
Unity Brush
- Jan 31 Thu 2013 23:44
基本的物件消失再出現另外一個遊戲物件
using System.Collections;
public class Evemy1 : MonoBehaviour
{
public static int Speed = 150; //掉下來數度
private float evemytime = 0.05f; //敵人時間
private float evemyspeed;
private float x, y, z; //座標
public GameObject EvemyDada; //新建遊戲物件
void Start(){
evemy();
}
// Update is called once per frame
void Update ()
{
if (evemytime <= Time.time) { //假設條件時間
transform.Translate (Vector3.down * Time.deltaTime * Speed); //物件往下掉
if (transform.position.z <= 235.0f){ //假設物件座標z軸
evemy();
}
}
}
void evemy(){
evemyspeed = Random.Range(100,500); //取亂數
x = Random.Range(-321,321);
y = 341f;
z = 752;
transform.position = new Vector3(x, y, z); 新建物件座標
}
void OnTriggerEnter (Collider c) //碰撞
if (c.tag == "Bullet) { //架設物件碰到物件(標籤名稱)
Instantiate (EvemyDada, transform.position, transform.rotation); //實體(物件, 本身物件座標, 物件旋轉座標)
}
Destroy (gameObject); //刪除物件
}
}
using UnityEngine;
using System.Collections;
public class Evemy1 : MonoBehaviour
{
public static int Speed = 150; //掉下來數度
private float evemytime = 0.05f; //敵人時間
private float evemyspeed;
private float x, y, z; //座標
public GameObject EvemyDada; //新建遊戲物件
void Start(){
evemy();
}
// Update is called once per frame
void Update ()
{
if (evemytime <= Time.time) { //假設條件時間
transform.Translate (Vector3.down * Time.deltaTime * Speed); //物件往下掉
if (transform.position.z <= 235.0f){ //假設物件座標z軸
evemy();
}
}
}
void evemy(){
evemyspeed = Random.Range(100,500); //取亂數
x = Random.Range(-321,321);
y = 341f;
z = 752;
transform.position = new Vector3(x, y, z); 新建物件座標
}
void OnTriggerEnter (Collider c) //碰撞
{
if (c.tag == "Bullet) { //架設物件碰到物件(標籤名稱)
Instantiate (EvemyDada, transform.position, transform.rotation); //實體(物件, 本身物件座標, 物件旋轉座標)
}
Destroy (gameObject); //刪除物件
}
}
- Jan 31 Thu 2013 23:23
基本的物件移動及發射物件
using UnityEngine;
using System.Collections;
public class SimpleShip : MonoBehaviour
{
public float speed; // 物件移動速度;
public int speed2; //發射物件數度;
public Rigidbody Projectile;
// Update is called once per frame
void Update ()
{
float abtToMove = Input.GetAxis ("Horizontal") * speed * Time.deltaTime; //輸入方向鍵及a,d 鍵 就水平移動
transform.Translate (Vector3.right * abtToMove); // 物件移動
if (transform.position.x >= 205.0f) // 假設物件移到調鍵x軸
transform.position = new Vector3 (-205.0f, transform.position.y, transform.position.y); // 物件 就重-x軸出現
else if (transform.position.x <= -205.0f) // 假設物件移到調鍵x軸
transform.position = new Vector3 (205.0f, transform.position.y, transform.position.y); // 物件 就重x軸出現
// 發射物件
if (Input.GetKeyDown ("space")) {
Rigidbody clone;
clone = (Rigidbody)Instantiate (Projectile, transform.position, transform.rotation);
clone.velocity = transform.TransformDirection(Vector3.up * speed2);
}
}
}
- Jan 31 Thu 2013 22:58
GUI.window及 Application.LoadLevel 方法
using UnityEngine;
using System.Collections;
public class MainCamera : MonoBehaviour
{
private bool sw = false; //另外視窗開關
void OnGUI ()
{
// 換場景
if (GUI.Button (new Rect (20, 20, 100, 50), "Play")) {
Application.LoadLevel("Startgame"); //場景名稱
}
if (GUI.Button (new Rect (20, 80, 100, 50), "Game Guide"))
sw = true;
if (sw == true)
GameGuide ();
else sw = false;
}
void GameGuide ()
{
GUI.Window (0, new Rect(360,30,400,300), DoMyWindow, "Game Guide"); // GUI.window(編號 , 位置 , 視窗底下方法 , "視窗名稱" )
}
// 寫入視窗方法
void DoMyWindow (int windowID)
{
GUI.Label(new Rect(10,20,100,20), "Computer keys: ");
GUI.Label(new Rect(30,40,100,20), "Laft: a");
GUI.Label(new Rect(30,60,100,20), "Right: d");
GUI.Label(new Rect(30,80,200,20), "Fired bullets: spacebar");
GUI.Label(new Rect(30,100,200,20), "Leave game: Esc / y");
if(GUI.Button(new Rect(150,260,100,20),"Exit")){
Application.LoadLevel("feelPlaygame");
}
}
}