1、控制玩家移动
public float speed = 10f; public float xMin = -6.5f; public float xMax = 6.5f; public float zMin = -4.5f; public float zMax = 4.5f; void FixedUpdate() { float h = Input.GetAxis("Horizontal"); float v = Input.GetAxis("Vertical"); Vector3 move = new Vector3(h, 0f, v); rigidbody.velocity = speed * move; //限制飞机活动范围,这个类似设置transform.position rigidbody.position = new Vector3( Mathf.Clamp(rigidbody.position.x, xMin, xMax), 0, Mathf.Clamp(rigidbody.position.z, zMin, zMax) ); }
2、设置边界(超过边界销毁对象(子弹、敌机))
DestroyByBoundary.cs脚本:
public void OnTriggerExit(Collider other) { Destroy(other.gameObject); }
3、设置陨石绕自身旋转
float tumble = 5; void Start() { //随机一个角速度 rigidbody.angularVelocity = Random.insideUnitSphere * tumble; }
4、速度正负控制移动方向正反
public float speed = 10f; //speed为正值,对象向前移动,为负值,向后移动 rigidbody.velocity = speed * transform.forward;
时间: 2024-10-09 10:59:57