Space Shooter 太空射击

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、设置边界(超过边界销毁对象(子弹、敌机))

Space Shooter 太空射击

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;

 

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-01-25
  • 2022-12-23
  • 2022-02-17
  • 2021-12-10
猜你喜欢
  • 2021-05-14
  • 2021-12-04
  • 2022-01-11
  • 2021-08-05
  • 2022-12-23
  • 2022-02-08
相关资源
相似解决方案