子弹伞状发射
搭建这样的场景,一个cube和一个Sphere,cube当作发射器,Sphere当作子弹,这里的Sphere通过Scale调节比例缩小,使其发射效果更好一些。
要写两个脚本,一个是绑在发射器上的,名为BulletLauncher,另一个是绑在子弹上的,名为BulletControl,子弹可以做成预制体。
BullteLauncher:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class BulletLauncher : MonoBehaviour
{
public GameObject cube;
public GameObject sphere;
private float timer;
// Start is called before the first frame update
void Start()
{
}
void Update()
{
timer+=Time.deltaTime;
if(timer>3)//当timer大于3时,发射子弹,使得发射有间隙
{
for(int i=0;i<5;i++)
{
Quaternion q=Quaternion.AngleAxis(-30+i*15,Vector3.up);//得到五种不同角度四
//元数
Vector3 newV=q*cube.transform.forward;//得到子弹需要旋转的方向
GameObject go=Instantiate(sphere);
go.ransform.position=cube.transform.position;
go.transform.rotation=Quaternion.LookRotation(newV);
}
timer=0;//timer重置为0
}
}
}
BulletControl :
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ButtleControl : MonoBehaviour
{
private float timer;
// Start is called before the first frame update
void Start()
{
)
void Upate()
{
transform.Translate(Vector3.forward*0.3f,Space.Self);//给子弹初始的方向的速度
}
}
运行结果:
子弹环形发射
与子弹伞状发射相近,就是将BulletLauncher稍改一下,如下:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class BulletLauncher : MonoBehaviour
{
public GameObject cube;
public GameObject sphere;
private float timer;
// Start is called before the first frame update
void Start()
{
}
void Update()
{
timer+=Time.deltaTime;
if(timer>3)//当timer大于3时,发射子弹,使得发射有间隙
{
for(int i=0;i<24;i++)
{
Quaternion q=Quaternion.AngleAxis(i*15,Vector3.up);//每15度作为一个子弹方向
Vector3 newV=q*cube.transform.forward;//得到子弹需要旋转的方向
GameObject go=Instantiate(sphere);
go.ransform.position=cube.transform.position;
go.transform.rotation=Quaternion.LookRotation(newV);
}
timer=0;//timer重置为0
}
}
}
运行结果:
来源:CSDN
作者:恶魔爱吃糖
链接:https://blog.csdn.net/weixin_43257287/article/details/104677308