抱歉一直未更新,因此特写一遍关于如何利用Unity的粒子系统制作相关弹幕的教程
该弹幕设置为针对玩家方向进行数条弹幕进行旋转。
可以直接将下面代码新建C#脚本附在粒子系统上即可,不过先确保粒子系统面板的设置注意事项:
1:粒子的Simulation Space为World ,这样以防位置信息发生改变时,弹幕也会随着改变:。
2:确保Emission不自动发射粒子,可以选择直接关闭或参数设置为0.
简单代码如下:
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class Danmu : MonoBehaviour {
public int damage = 50;//弹幕粒子伤害
public float speed = 20;//弹幕粒子运行速度
public float barrageLifeTime = 10;//粒子存活时间
private float intervalTime = 0.2f;//发射间隔时间
public int LineNumber = 8; //多少条弹幕
private ParticleSystem emitter;//父粒子系统
private ParticleSystem.Particle[] particles;
private float emitterParticleSize = 1.0f;//粒子设置大小
private float time;
private Transform player;//玩家对象,区分粒子运动是否针对玩家方向
private List<Vector3> orginalPositions;//初始位置
private List<Color> orginalColors;//初始颜色
// Use this for initialization
void Start () {
emitter = GetComponent<ParticleSystem>();
orginalPositions = new List<Vector3>();
orginalColors = new List<Color>();
float angle = (2 * Mathf.PI) / LineNumber;
for (int i = 0; i < LineNumber; i++)
{
float x = Mathf.Cos( angle * i ) ;
float z = Mathf.Sin( angle * i );
orginalPositions.Add( new Vector3( x, z, 0 ) );
orginalColors.Add( ColorBasic(( (float)i)/LineNumber ) );
}
}
// Update is called once per frame
void Update () {
Quaternion rotation = Quaternion.Euler( 0, Time.time * 10, 0 ); //自身旋转
Vector3 direction = player.position - transform.position;
Quaternion rotationTarget = Quaternion.LookRotation( direction );
if (Time.time > time)
{
time = Time.time + intervalTime;
for(int i=0 ;i<LineNumber;i++)
{
Vector3 direction = rotation *rotationTarget* orginalPositions[i];
Fire( direction, speed, barrageLifeTime, orginalColors[i] );
}
}
}
void Fire ( Vector3 _direction, float _speed, float _lifeTime,Color _color )
{
particles = new ParticleSystem.Particle[1];
particles[0].velocity = _direction.normalized * _speed;
particles[0].color = _color;
emitter.Emit( transform.position, particles[0].velocity, emitterParticleSize, _lifeTime, particles[0].color );
}
void Fire ( Vector3 _position, Vector3 _direction, float _speed, float _lifeTime, Color _color )
{
particles = new ParticleSystem.Particle[1];
particles[0].position = _position;
particles[0].velocity = _direction.normalized * _speed;
particles[0].color = _color;
emitter.Emit( particles[0].position, particles[0].velocity, emitterParticleSize, _lifeTime, particles[0].color );
}
void OnParticleCollision ( GameObject other )
{
if (other.layer == 10) //玩家layer设置值
{
// player.GetComment<playerLife>().life -= damage;//如果玩家有血值的话
}
if (other.rigidbody != null)
{
Vector3 direction = other.transform.position - transform.position;
other.rigidbody.AddForce( direction.normalized * 10 );
}
}
Color ColorBasic ( float _a )
{
float x = Mathf.Cos( _a );
float y = Mathf.Cos( _a +Mathf.PI/2);
float z = x * y ;
float r = x * 0.5f + 0.5f;
float g = y * 0.5f + 0.5f;
float b = z* 0.5f + 0.5f;
return new Color( r, g, b );
}
}
该弹幕设置为针对玩家方向进行数条弹幕进行旋转。
可以直接将下面代码新建C#脚本附在粒子系统上即可,不过先确保粒子系统面板的设置注意事项:
1:粒子的Simulation Space为World ,这样以防位置信息发生改变时,弹幕也会随着改变:。
2:确保Emission不自动发射粒子,可以选择直接关闭或参数设置为0.
简单代码如下:
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class Danmu : MonoBehaviour {
public int damage = 50;//弹幕粒子伤害
public float speed = 20;//弹幕粒子运行速度
public float barrageLifeTime = 10;//粒子存活时间
private float intervalTime = 0.2f;//发射间隔时间
public int LineNumber = 8; //多少条弹幕
private ParticleSystem emitter;//父粒子系统
private ParticleSystem.Particle[] particles;
private float emitterParticleSize = 1.0f;//粒子设置大小
private float time;
private Transform player;//玩家对象,区分粒子运动是否针对玩家方向
private List<Vector3> orginalPositions;//初始位置
private List<Color> orginalColors;//初始颜色
// Use this for initialization
void Start () {
emitter = GetComponent<ParticleSystem>();
orginalPositions = new List<Vector3>();
orginalColors = new List<Color>();
float angle = (2 * Mathf.PI) / LineNumber;
for (int i = 0; i < LineNumber; i++)
{
float x = Mathf.Cos( angle * i ) ;
float z = Mathf.Sin( angle * i );
orginalPositions.Add( new Vector3( x, z, 0 ) );
orginalColors.Add( ColorBasic(( (float)i)/LineNumber ) );
}
}
// Update is called once per frame
void Update () {
Quaternion rotation = Quaternion.Euler( 0, Time.time * 10, 0 ); //自身旋转
Vector3 direction = player.position - transform.position;
Quaternion rotationTarget = Quaternion.LookRotation( direction );
if (Time.time > time)
{
time = Time.time + intervalTime;
for(int i=0 ;i<LineNumber;i++)
{
Vector3 direction = rotation *rotationTarget* orginalPositions[i];
Fire( direction, speed, barrageLifeTime, orginalColors[i] );
}
}
}
void Fire ( Vector3 _direction, float _speed, float _lifeTime,Color _color )
{
particles = new ParticleSystem.Particle[1];
particles[0].velocity = _direction.normalized * _speed;
particles[0].color = _color;
emitter.Emit( transform.position, particles[0].velocity, emitterParticleSize, _lifeTime, particles[0].color );
}
void Fire ( Vector3 _position, Vector3 _direction, float _speed, float _lifeTime, Color _color )
{
particles = new ParticleSystem.Particle[1];
particles[0].position = _position;
particles[0].velocity = _direction.normalized * _speed;
particles[0].color = _color;
emitter.Emit( particles[0].position, particles[0].velocity, emitterParticleSize, _lifeTime, particles[0].color );
}
void OnParticleCollision ( GameObject other )
{
if (other.layer == 10) //玩家layer设置值
{
// player.GetComment<playerLife>().life -= damage;//如果玩家有血值的话
}
if (other.rigidbody != null)
{
Vector3 direction = other.transform.position - transform.position;
other.rigidbody.AddForce( direction.normalized * 10 );
}
}
Color ColorBasic ( float _a )
{
float x = Mathf.Cos( _a );
float y = Mathf.Cos( _a +Mathf.PI/2);
float z = x * y ;
float r = x * 0.5f + 0.5f;
float g = y * 0.5f + 0.5f;
float b = z* 0.5f + 0.5f;
return new Color( r, g, b );
}
}