unity-game-enginerendererparticle-system

How to render a trail for a non-moving object in Unity3D


In my game, my player (a ball) doesn't move in the x-axis. The ball just can jump (y-axis).

The level is always translating in the x-axis and give to the player the sensation the ball is moving.

Is it possible to add a trail renderer (or something like this) to my ball? To enforce the sensation of moving of the ball.

the image: the effect I want to have
(source: barouch.fr)


Solution

  • Yes you can, and I've proven that it is possible.

    enter image description here

    As you can see in the image, I nested 5 spheres recursively, each with Z + 1 of its parent.
    You're going to need rigidbody for each of them because we're going to AddForce to them.

    And here's the important part, the scripts.
    I have 3 scripts, named HeadScript, BodyScript, TailScript (kinda like a snake game, so I name them like that)

    HeadScript:

    void Update () {
        if(Input.GetKeyDown(KeyCode.Space)){
            rigidbody.AddForce(new Vector3(0, 6, 0), ForceMode.Impulse);
            GameObject child = transform.FindChild("Sphere").gameObject;
            BodyScript childScript = child.GetComponent<BodyScript>();
            childScript.ChildJump();
        }
    }
    

    HeadScript is only attached to the head (the rightmost sphere), it needs to detect keydown (for jumping), then we add force to itself (only upwards) so it looks like it's jumping. Afterwards, get the child object, (the ball on its left) and call the function ChildJump which is inside BodyScript.

    BodyScript:

    public void ChildJump(){
        Invoke ("Jump", 0.1f);
    }
    
    void Jump(){
        rigidbody.AddForce(new Vector3(0, 6, 0), ForceMode.Impulse);
        GameObject child = transform.FindChild("Sphere").gameObject;
        if(child.GetComponent<BodyScript>() == null){
            TailScript childScript = child.GetComponent<TailScript>();
            childScript.ChildJump();
        }
        else {
            BodyScript childScript = child.GetComponent<BodyScript>();
            childScript.ChildJump();
        }
    }
    

    BodyScript is attached to every sphere except the first and the last. Parent objects call the function ChildJump which contains only 1 function, Invoke.

    Invoke is used to call a function after a certain amount of delay. In this code the delay is 0.1s.

    The Jump itself will call its child's (the ball on its left) ChildJump function. Now you see why it is kind of recursive.

    We added the if condition so we won't get any errors when we get BodyScript component from the last sphere, which isn't there. The last script has only TailScript.

    TailScript:

    public void ChildJump(){
        Invoke ("Jump", 0.1f);
    }
    
    void Jump(){
        rigidbody.AddForce(new Vector3(0, 6, 0), ForceMode.Impulse);
    }
    

    The final script, TailScript only contains AddForce to itself, because it has no child.

    Every sphere (except the head) will jump after 0.1s delay from its parent, becoming what you call it, trail renderer.

    Now what's left is just to add alpha material for each of them so they become more transparent, and voila! Job's done!