Unity 3D Code Snippet – Inheriting Parent Velocity

Imagine that you’re running at 10 MPH. While you’re running, you throw a ball at 5 MPH. Now, imagine the laws of physics are screwed up and the ball, instead of adding your running velocity to its own, only moves at its 5 MPH. Imagine the look on your face when you try to figure out how the heck you just did that. Imagine the towns’ people accusing you of being a witch and burning you at the stake. Gruesome.

This is (sorta) what happened to me. I instantiated particles, but they didn’t pick up the instantiators’ velocity. Well, here’s a handy script I found, and now I am sharing it to help get your game up to speed (Ugh. I rather just be burned at the stake than to hear another corny pun).

The original script I found was posted as an answer to another persons’ problem on Unity Answers. We happen to have had the same problem, and this worked like a charm for me, I just added a slight modification. Here is a post to the original question and answer. Ken 2 (The dude who answered the question) gave pretty good details on how to use 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.

Simple, yes. My only modification is the option to NOT explicitly add a RigidBody. I made mine so it grabs the top-most root transform if no RigidBody is declared.

using UnityEngine;
using System.Collections;

public class InheritParentVelocity : MonoBehaviour
{

    public Rigidbody target = null;
    private ParticleEmitter theEmitter;
    public bool inheritParentVelocity = true;

    void Start()
    {
        theEmitter = transform.GetComponent();
    }    void Update()
    {
        if (inheritParentVelocity)
        {
            if (target != null)
            {
                theEmitter.worldVelocity = target.velocity;
            }
            else
            {
                theEmitter.worldVelocity = transform.root.rigidbody.velocity;
            }
        }
        else
            theEmitter.worldVelocity = Vector3.zero;
    }
}

After that, you just need to set up your particle system. I changed the Emitter Velocity Scale to 1, set Simulate in World Space to true, and changed the Local Velocity in the direction I wanted it to go. It turned out to be very handy, and I wanted to share it with everyone, so you may solve… some.. problem you may be having… involving velocities. Yeah. Anyway, take care.

Advertisements

Published by

KeithM

Computer Scientist for Engility, and Indie Game Dev from New Jersey. Loves playing games, and loves making them, too.

4 thoughts on “Unity 3D Code Snippet – Inheriting Parent Velocity”

  1. This would be useful for instances where you can’t use the PE’s built in ability to directly inherit the parent transform’s velocity.
    If you don’t mind saying, what were the particles for that you were instantiating the emitters rather than activating them?

    1. They’re for tracer bullets. I have to say, though, I used instantiation as an example for the tutorial. I have my tracer bullet emitter attached to my gun, and I’m just modifying the enabled bool. However, when the particles emit, they’re moving at a slower velocity than the parent emitting them, so I used this script to modify the particle’s speed and have it sync with the parent object.

  2. Ah, let me guess, the direct parent of the emitter didn’t have a rigidbody? Are these bullets slow moving or high speed? It might work better to raycast the hit instead of relying on the world particle collider, and then use a trail renderer to draw your tracer.
    Either way, it sounds cool 😉

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s