Possible To Add A Wiggle Effect On A Missile - Help

Hello,

I have made a homing missile, and I was just wondering how I could add a wiggle effect on the missile as it follows its target.

This is a snippet of the script that moves the missile:

transform.LookAt(target);
    transform.Translate(Vector3.forward*speed*Time.deltaTime);
    Invoke("SelfDestroy", Timer);

Please help,

Thanks

In addition to the forward vector that you've already coded, you could add a lateral movement based on a sine wave. Something like:

transform.Translate(Vector3.right * sin(wiggleRate * Time.time) * wiggleMagnitude);

With a little more math you can get it to start and end with wiggle=0, so it looks like it was launched from a single spot, and hits the exact target.

You also may need to rotate the missile to point in the direction that it's moving that frame, otherwise it may look like it's sliding left and right for no reason. (Depends on the game and what the missile looks like.)

So that's a way to add wiggle. But if you want it to feel like the missile is steering towards the target, possibly overshooting and then turning back, with a Newtonian feel to it, then you'll probably need to start over and calculate the missile's movement using forces. The missile would have a turning rate, and its rocket would push it forward, and there might be air resistance to prevent it from going faster and faster each frame. The wiggle would come from it constantly turning towards to face the target.

Try this C# code. I don't know if this is what you're looking for but it was pretty fun watching it go off. Even more fun if you attach the camera to the rocket :)

using UnityEngine;
using System.Collections;

public class HomingRocket : MonoBehaviour
{
    public Transform target;
    public float rocketTurnSpeed = 90.0f;
    public float rocketSpeed = 10.0f;
    public float turbulence = 10.0f;

    void Update ( )
    {
        Vector3 direction = Distance( ) + Wiggle( );
        direction.Normalize( );

        transform.rotation = Quaternion.RotateTowards( transform.rotation, Quaternion.LookRotation( direction ), rocketTurnSpeed * Time.deltaTime );
        transform.Translate( Vector3.forward * rocketSpeed * Time.deltaTime );
    }

    private Vector3 Distance ( )
    {
        return target.position - transform.position;
    }

    private Vector3 Wiggle ( )
    {
        return Random.insideUnitSphere * turbulence;
    }
}