一、Unity 指定区域随机实例化预制体Prefab 代码
using UnityEngine;
public class NewBehaviourScript : MonoBehaviour {
public GameObject prefab;
void Update()
{
//位置
float x = Random.Range(-10, 10);
float y = Random.Range(-10, 10);
float z = Random.Range(-10, 10);
Vector3 pos = new Vector3(x,y,z);
//实例化
Instantiate(prefab,pos,Quaternion.identity);
}
}
Prefab
效果:
脚本绑定到到物件,把任意prefab拖到脚本中的Prefab的参数上,运行,每一帧都在空间内随机生成一个prefab的实例。
参考:
二、unity-实现摄像机跟随物体(Vector3.SmoothDamp)
using UnityEngine;
using System.Collections;
public class FllowTarget : MonoBehaviour {
public Transform target; //摄像机要跟随的人物
public float smoothTime = 0.01f; //摄像机平滑移动的时间
private Vector3 cameraVelocity = Vector3.zero;
private Camera mainCamera; //主摄像机(有时候会在工程中有多个摄像机,但是只能有一个主摄像机吧)
void Awake ()
{
mainCamera = Camera.main;
}
void Update()
{
transform.position = Vector3.SmoothDamp(transform.position, target.position + new Vector3(0, 0, -5), ref cameraVelocity, smoothTime);
}
}
Vector3.SmoothDamp
Vector3.SmoothDamp:
Vector3.SmoothDamp 平滑阻尼
static function SmoothDamp (current : Vector3, target : Vector3, ref currentVelocity : Mathf.Infinity, deltaTime : float = Time.deltaTime) : Vector3
Parameters参数
current
当前的位置
target
我们试图接近的位置
currentVelocity
当前速度,这个值由你每次调用这个函数时被修改
smoothTime
到达目标的大约时间,较小的值将快速到达目标
maxSpeed
选择允许你限制的最大速度
deltaTime
自上次调用这个函数的时间。默认为 Time.deltaTime
Description描述
随着时间的推移,逐渐改变一个向量朝向预期的目标。
向量由一些像弹簧阻尼器函数平滑,这将永远不会超过。最常见的用途是平滑跟随相机。
参考:https://www.it610.com/article/1280425870741225472.htm
三、Unity计算两点之间的距离
向量的长度是用勾股定理计算出来,计算机计算两次方和开根的运算量比加减法要费时的多。
所以求两个对象的距离最常用的是比较两点距离的平方:
float dis = (transform.position - player.position).sqrMagnitude
勾股定理
参考:
四、unity绕定点循环(RotateAround)
三个方法:
RotateAround
参考:
五、物体运动速度(rigibody.velocity)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Randomcoloc :MonoBehaviour{
public Rigidbody rig:
//Use this for initialization
void Start(){
rig = GetComponent<Rigidbody>();
}
//Update is called once per frame
void Update()
{
if(Input.GetMouseButtonUp(0))
get_speed();
}
public void get_speed(){
float temp_speed = rig.velocity.magnitude;
Debug.Log(temp_speed);
}
}
rigibody.velocity
六、游戏物体的销毁(GameObject.Destroy)
GameObject.Destroy(gameObject,2.0f);
Destroy
七、Unity取随机数的用法
功能:随机取五个数,求他们的平均数,保留两位小数
void Start()
{
quiz1 = Random.Range(0f, 100f); //取0-100之间的随机数
quiz2 = Random.Range(0f, 100f);
quiz3 = Random.Range(0f, 100f);
quiz4 = Random.Range(0f, 100f);
quiz5 = Random.Range(0f, 100f);
float average = (quiz1 + quiz2 + quiz3 + quiz4 + quiz5) / 5;
average = Mathf.Round(average * 100f) / 100f; //如果是保留三位小数--Mathf.Round(average * 100f) / 1000f
Debug.Log("Average:" + average);
}
Random.Range
参考:
八、Unity3d C#实现获取两个对象的夹角值(0--360)含源码
原文链接:
九、Unity在一个父物体下实例化子物体
public GameObject hj5; public GameObject hj10; private GameObject GetHand;
private GameObject szhj5;
private GameObject szhj10;
public void ShowSZHJ(bool sz5)
{
GetHand = GameObject.Find("ARCamera");
if(sz5)
{
szhj5 = Instantiate(hj5);
szhj5.transform.parent = GetHand.transform;
szhj5.transform.localPosition = new Vector3(0, 0, 8);
}
if(!sz5)
{
szhj10 = Instantiate(hj10);
szhj10.transform.parent = GetHand.transform;
szhj10.transform.localPosition = new Vector3(0, 0, 3 );
}
}
View Code
//首先先分配一个内存(随便起个名字都可以看需要)加载一个实例,这个实例名字就是我们需要的物体,这个物体必须是个预制体Prefab
//接着将这个物体实例化成GameObject,让他具有GameObject的属性
//让他成为一个物体的子物体
GameObject 名字1 = ObjectReference.LoadPrefab("需要实例化的物体名字");
GameObject 名字2= Instantiate(名字1) as GameObject;
eAI.transform.parent = 父物体名字.transform;
十、Unity 退出游戏 方法
Application.Quit();
十一、unity禁用物体或组件
BoxCollider collider = GetComponent<BoxCollider>();
collider.enabled = false;//禁用碰撞组件
learn02.enabled = false;//禁用脚本
learn02.ShowMessage();//禁用了也可以调用方法
Rigidbody rgd = player.GetComponent<Rigidbody>();
rgd.mass = 100;//刚体的质量
组件的启用与禁用
gameObject.activeInHierarchy//返回当前物体是否启用
gameObject.SetActive (true);//将物体启用
gameObject.SetActive (false);//将物体禁用
物体的启用与禁用