標籤:
地形案例總結
變換 Transform
1 變換組件決定了情境中所有物體的方位,旋轉和縮放。每個物體都有一個變換組件。
2 //擷取遊戲對象的子物體
ston = GameObject.Find("Capsule").transform.FindChild("skeleton");
在Unity中父子化是一個非常重要的概念。當一個遊戲對象是另一個遊戲對象的父物體時,其子遊戲對象會隨著它移動、旋轉和縮放,就像你的胳膊屬於你的身體,當你旋轉身體時,你的胳膊也會跟著旋轉一樣。任何物體都可以有多個子物體,但只能有一個父物體。
if (Input.GetKey(KeyCode.W))
{
ston.animation["run"].speed = 1;
ston.animation.Play("run");
transform.Translate(new Vector3(0, 0, 1));
}
射線和碰撞檢測
碰撞檢測:
需要注意:
1 需要給找到的遊戲對象添加name屬性:
//給物體添加name屬性
cubeLon.name = "cubeLon";
2 指令碼應該添加給要碰撞檢測的物體
#region 碰撞檢測
//碰撞檢測有三個函數enter,stay,exit
void OnCollisionEnter(Collision collision)
{
//通過物體的名稱找到碰撞物體並銷毀
if (collision.gameObject.name == "cubeLon")
{
Destroy(collision.gameObject);
Debug.Log("碰撞到的物體的名字是:" + collision.gameObject.name);
}
}
#endregion
射線
射線是一個無窮的線,開始於origin並沿著direction方向。
Camera.ScreenPointToRay 螢幕位置轉射線
返回一條射線從攝像機通過一個螢幕點。
產生的射線是在世界空間中,從相機的近裁剪面開始並穿過螢幕position(x,y)像素座標(position.z被忽略)。
if (Input.GetMouseButtonUp(0))
{
//cube.rigidbody.AddForce(new Vector3(1, 0, 0) * speed, ForceMode.Impulse);
#region 射線
//找到攝像機和滑鼠位置
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
//如果點擊物體存在
if (Physics.Raycast(ray, out hit))
{
Destroy(hit.collider.gameObject);
}
#endregion
}
使用滑鼠點擊實現物體移動
Vector3.ClampMagnitude 限制長度
static function ClampMagnitude (vector : Vector3, maxLength : float) : Vector3
返迴向量的長度,最大不超過maxLength所指示的長度。
Vector3.Distance 距離
static function Distance (a : Vector3, b : Vector3) : float
返回a和b之間的距離。
if (Input.GetMouseButton(0))
{
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
if (Physics.Raycast(ray,out hit))
{
target = hit.point;
}
float distance = Vector3.Distance(target, transform.position);
if (distance>=2)
{
Vector3 step = Vector3.ClampMagnitude((target - transform.position), speed);
chara1.Move(step);
}
}
打箱子代碼(打箱子遊戲)
記錄時間
Time.realtimeSinceStartup;
using UnityEngine;
using System.Collections;
public class boxHit : MonoBehaviour {
public GameObject box;
public float power = 15.0f;
public GameObject fire;
//public Texture2D
public Texture2D Cursor;
public Texture2D bg;
public Texture2D fg;
private float pressTime;
private float now;
// Use this for initialization
生產箱子:
void Start () {
for (int i = 0; i < 5; i++)
{
for (int j = 0; j < 5; j++)
{
GameObject box1 = GameObject.Instantiate(box) as GameObject;
box1.name = "boxHit";
box1.transform.position=new Vector3(j,0.5f+i,0);
}
}
// Screen.showCursor = false;
}
// Update is called once per frame
void Update () {
按下時記錄時間
if (Input.GetMouseButtonDown(0))
{
pressTime = Time.realtimeSinceStartup;
}
if (Input.GetMouseButtonUp(0))
{
pressTime = 0;
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
if (Physics.Raycast(ray,out hit))
{
GameObject bullet = GameObject.CreatePrimitive(PrimitiveType.Sphere);
bullet.transform.localScale = new Vector3(0.5f, 0.5f, 0.5f);
bullet.renderer.material.color = Color.blue;
bullet.transform.position = Camera.main.transform.position;
Vector3 len = hit.point - Camera.main.transform.position;
Rigidbody rb = bullet.AddComponent<Rigidbody>();
//rb.AddForce()
rb.AddForce(len*power, ForceMode.Impulse);
boxRender br = bullet.AddComponent<boxRender>();//相當於new了一個對象
br.fire = fire;
//bullet.AddComponent<DestroyInVisible>();
bullet.AddComponent<destoryBox1>();
}
power = 15.0f;
}
}
void OnGUI()
{
float left = Input.mousePosition.x-Cursor.width/2;
float top = Screen.height - Input.mousePosition.y-Cursor.height/2;
GUI.DrawTexture(new Rect(left, top, Cursor.width, Cursor.height), Cursor);
if (pressTime>0)
{
now = Time.realtimeSinceStartup;
float len = now - pressTime;
float percent = len / 10.0f;
GUI.DrawTexture(new Rect(0, 0, 100, 20), bg);
if (percent>1)
{
percent = 1;
}
GUI.DrawTexture(new Rect(0, 0, 100 * percent, 20), fg);
power = 15.0f + percent* 10.0f;
}
}
}
打箱子之火花類
using UnityEngine;
using System.Collections;
public class boxRender : MonoBehaviour {
public GameObject fire;
void OnCollisionEnter(Collision collision)
{
if (collision.collider.gameObject.name=="boxHit")
{
GameObject fire1 = GameObject.Instantiate(fire) as GameObject;
fire1.name = "fireBullet";
fire1.transform.position = collision.collider.gameObject.transform.position;
Destroy(fire1, 1);
}
}
}
打箱子&地形&滑鼠點擊移動總結