- Home /
Problem matching particle & object velocities + speed
I have a scene with a ship that currently has an ellipsoid emitter attached to fire particles (as projectiles) directly forward using the emitter's Local Velocity value. This works fine and the emitter rotates along with the ship when it is stationary, however once the ship is moving the particles either lag behind, travel too fast ahead of the ship, or a combination of the two (varying with each particle, I think that may just be a glitch).
I tried setting the Emitter Velocity Scale to 1, hoping they would begin with the ship's velocity and then add their own Local Velocity value to that in order to maintain the same speed as when stationary, however this creates the unwanted acceleration. Am I right in thinking this is because I'm using AddForce to accelerate my ship rather than changing its velocity directly? Note that this happens even when no force is being added and the ship is simply continuing forward at its current velocity. If AddForce is the cause, is there any way to continue to use it and still keep the particle velocity in line with the ship velocity?
Thanks.
Answer by ken 2 · Apr 27, 2011 at 03:14 AM
Oh, of course as soon as I commented I realized how to do it. I made a script that sets the world velocity of the emitter (ie the gun) to the same as the target (ie the ship). Add the script below to the same gameobject as your emitter, and drag the 'ship' into it as the target. In this case it's a rigidbody because it's easy to get the velocity.
public class InheritParentVelocity: MonoBehaviour {
public Rigidbody target;
private ParticleEmitter theEmitter;
public bool inheritParentVelocity=true;
void Start()
{
theEmitter=transform.GetComponent<ParticleEmitter>();
}
void Update ()
{
if(inheritParentVelocity)
theEmitter.worldVelocity = target.velocity;
else
theEmitter.worldVelocity = Vector3.zero;
}
}
Answer by qJake · Jun 02, 2010 at 02:14 AM
I'm not sure if this is the effect you want, because I had a hard time understanding your explanation, but try (un)checking the Simulate in worldspace? checkbox. This means your particles will basically either exist in global or local space, and it sounds like you want the latter.
Sorry about the explanation, it's quite difficult to put into words. I need simulate in worldspace checked because the emitter is meant to be a gun, with each particle representing a single shot. The problem is that the velocity of the particles generated once the ship has any velocity at all doesn't inherit the velocity of the ship as the baseline and then add the particles' own local velocity to that. It seems like it's taking the ship's velocity and multiplying it by something, only I don't know what. Description of setup follows:
Duplicate this by creating an object moving along a single axis & adding an elliptical emitter with $$anonymous$$/max energy of 5, local velocity of 2 (along the same axis as the object's movement, z in my case), emitter velocity scale of 1, and allowing the moving object to rotate (taking the emitter with it) without altering its direction of movement (ie it can spin left and right as it moves forward). As you speed up the object, the speed of the particles increases on a curve. The particle stream also keeps following the object's movement vector rather than the axis assigned in local velocity.
Oh, you want to add the velocity of your moving ship to the particles, yet still have them exist in world space, I understand... did you try adding your particle emitters as child game objects, and then attaching those to the ship? Beyond that, you'd probably have to script the particles manually, calculating the resulting force vector for the particles based on the ship's movements.
Yes that's what I'm after. The emitter is attached to an empty gameobject that is a child of my ship, and that's what gave me this result. Scripting the particles sounds like rather a lot more work than just instantiating a plane with my particle texture on it and giving that a particular constant force from birth. I'll probably have to do it the hard way in the end anyway though.. Thanks for your help :)
Question: How would I solve this same problem, but have the instantiated object exist in local space to the ship? I'm having issues getting this to work. When I instantiate the object it doesn't parent itself to the launcher (I have attached to the ship object), but that's what I'd like it to do. Is there a way to instantiate a rigidbody as a child of the launcher, so that all it's transformations happen relative to the ship itself? I describe my problem a little more at:
http://answers.unity3d.com/questions/18553/proper-inertia-on-projectile-in-starfox-like-game
Your answer
Follow this Question
Related Questions
Obtaining Constant Speed with AddForce 1 Answer
Constant force motion: s = s0 + v0 * t + a0 * t * t. Why? 1 Answer
Accelerate a rigidbody towards max speed 2 Answers
different beetween transform.translat and rigidbody.addforce? 1 Answer
How do i make the Impulse i give to my Player not be lowered by acceleration of gravity? 1 Answer