《Unity游戏设计与实现》学习笔记一
Unity 5.0后的版本中,使用rigidbody的方法:
this.GetComponent<Rigidbody> ()
例如:点击鼠标左键使物体向上跳。
public class Player : MonoBehaviour { protected float jump_speed = 8.0f; void Update () { if(Input.GetMouseButtonDown(0)){ this.GetComponent<Rigidbody> ().velocity = Vector3.up * this.jump_speed; } }
碰撞事件检测:
void OnCollisionEnter(Collision collision){ if(collision.gameObject.tag == "Floor"){ this.is_landing = true; } }
注释:当碰撞的对象标签是“Floor”时,实现代码this.is_landing = true。
游戏对象不再被绘制时,调用该方法删除对象:
void OnBecameInvisible () { Destroy (this.gameObject); }
实例化:
public class Launcher : MonoBehaviour { public GameObject ballPrefab; void Update () { if(Input.GetMouseButtonDown(1)){ Instantiate (this.ballPrefab); } } }
注释:点击鼠标右键,实例化ballPrefab。