- Home /
make particles stick to a moving target
I am making a 2d shooter and I use Raycast for my gun. If target is hit it creates particles at that location. My question is how to I make particles stick to a moving target??? Gun Code:
Var Gun: GameObject;
var FireDelay: float = 0.0;
var BulletPrefab : GameObject;
private var WeaponTimer : float = 0.0;
function Update () {
if ( Input.GetButton( "Fire1" ) && WeaponTimer >= FireDelay ) {
weapon_Fire_Timer = 0.0;
var hit : RaycastHit;
if ( Physics.Raycast( Gun.transform.position, Gun.transform.forward, hit ) ) {
Instantiate( BulletPrefab, hit.point, Quaternion.LookRotation( hit.normal ) );
}
}
WeaponTimer += Time.deltaTime;
Answer by john-essy · Nov 04, 2011 at 10:07 PM
What i do is when i create the bullet as a prefab all i do is attach the particle effect to the bullet. Then when the bullet is created the particles are there also
Sure. But, the code above is "firing" the bullet with a raycast, meaning it pretends to travel between frames and has no actual bullet model. The spawn is at hit.point
-- where the bullet hits, doesn't travel from you to the target. BulletPrefab
is probably a spang, flames or blood or something.
you are right BulletPrefab is just a "Fire Effect" Particle System
Answer by Owen-Reynolds · Nov 04, 2011 at 10:10 PM
Can make particles stick to the emitter by unchecking Simulate in worldSpace
. Can make the emitter follow around what you shot by childing to it:
// untested javascript: // [Edit: in response to comment]
// spawn the emitter and save a handle to it: var hitEffect = Instantiate( BulletPrefab, ... );
// hit.transform is the thing you hit. // make the newly spawned emitter be a child of what you hit; // Emitter should "stick" to the target and follow it around: hitEffect.parent = hit.transform;
Sorry I am lost a bit… Would BulletPrefab (Particle) be Child and Target would be parent???
Your answer
![](https://koobas.hobune.stream/wayback/20220613032557im_/https://answers.unity.com/themes/thub/images/avi.jpg)