地形案例总结
变换 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);
}
}
}