Ctrip call
void Start()
{
print("task1");
print("task2");
//print("task3");
StartCoroutine("Task3");//开启携程方法
print("task4");
}
void Update()
{
if (Input.GetKey(KeyCode.Space))
{
StopCoroutine("Task3");
}
}
IEnumerator Task3()
{
yield return new WaitForSeconds(2f);//停顿2s
print("task3");
}
Rigid body movement (virtual axis)
Still can not forget the finished "parameter" r pass objects to write their own
private Rigidbody r;
// Start is called before the first frame update
void Start()
{
r = gameObject.GetComponent<Rigidbody>();
}
// Update is called once per frame
void Update()
{
//刚体的移动
float h = Input.GetAxis("Horizontal");
float v = Input.GetAxis("Vertical");
r.MovePosition(r.position+transform.right * h * Time.deltaTime * 2f);
r.MovePosition(r.position + transform.forward * v * Time.deltaTime * 2f);
}
Collision events
It is a collection of collision impact parameter, which has all the features you need collision information
//碰撞事件检测
private void OnCollisionEnter(Collision collision)
{
if (collision.gameObject.name!="Plane")
{
print("Enter: " + collision.gameObject.name);
}
}
private void OnCollisionExit(Collision collision)
{
if (collision.gameObject.name!="Plane")
{
print("Exit:" + collision.gameObject.name);
}
}
private void OnCollisionStay(Collision collision)
{
if (collision.gameObject.name!="Plane")
{
print("Stay: " + collision.gameObject.name);
}
}
Trigger Trigger
After the collision is a trigger, box collider inside the check is trigger, impactor into a flip-flop.
Collision triggered three conditions:
//trigger事件,运动的物体必须带有collider和rigidbody
//另一个物体必须至少有collider
//其中一个勾选IsTrigger
private void OnTriggerEnter(Collider other)
{
print("triggerEnter: " + other.gameObject.name);
}
private void OnTriggerExit(Collider other)
{
print("triggerExit: " + other.gameObject.name);
}
private void OnTriggerStay(Collider other)
{
print("triggerStay: " + other.gameObject.name);
}
}
Prefabricated destroyed body ( life )
In the creation of the preform you will often need to add a life that automatically destroy destroy ().
Can be found in the prefabs (preform own folder) in the preform, open prefab view
can also be the object> direct cut over to see
void Start()
{
Destroy(gameObject, 0.2f);//活不过2s
}
Physical ray
The same mechanism is triggered.
// Start is called before the first frame update
private Ray ray;
private RaycastHit hit;
public GameObject obj;
// Update is called once per frame
//创建一条射线
//检测射线与其他物体的碰撞,得到碰撞信息
//通过碰撞信息对物体进行处理
void Update()
{
//鼠标左键按下发射射线
if (Input.GetMouseButtonDown(0))
{
//使用主摄像机创建一条射线,射线方向是到鼠标点击位置
ray = Camera.main.ScreenPointToRay(Input.mousePosition);
//使用物理类中的射线检测方法
if (Physics.Raycast(ray,out hit))
{
Instantiate(obj, hit.collider.transform.position, hit.collider.transform.rotation);
//将碰撞到的物体销毁
GameObject.Destroy(hit.collider.gameObject);
}
}
}
These are my dish (escape) to learn unity code notes