U3D 实现子弹发射效果

时间:2023-03-08 19:23:39
U3D 实现子弹发射效果

首先,这里子弹要模拟的相似的话,用2D刚体比较好,会有重力,*落体运动。

using UnityEngine;
using System.Collections; public class gun : MonoBehaviour { public Rigidbody2D rocket;
public float speed;
// Use this for initialization void Start () { } // Update is called once per frame
void Update () {
if(Input.GetKey(KeyCode.K)){
不断的实例化子弹刚体,并让其向前运动。
Rigidbody2D bullet = Instantiate(rocket,transform.position,Quaternion.Euler(new Vector3(0,0,0))) as Rigidbody2D;
//使刚体运动 Y方向为0
bullet.velocity = new Vector2(speed,0); }
}
}

  效果如图:

U3D 实现子弹发射效果