Game Development Community

Spawn a "Magic" Bullet From Thin Air

by Steve Acaster · in Torque 3D Professional · 02/18/2011 (1:18 pm) · 0 replies

Sometimes, you might want the player to take a shot to keep them on their toes, or spawn a grenade to get thrown at the player. eg: The player moves towards a house and a grenade or shot comes from a darkened window.

Maybe you don't want to trust an Ai with this delicate task of stressing the player out at just the right moment, so why not spawn a "magic bullet" to come out of nowhere.

In the below code example, we'll use stock assets.

Somewhere in your scripts add this:

function magicBullet(%obj)
{
%aim = %obj.getForwardVector();

%vector = VectorScale(%aim, 100);

// Create magic bullet!
	%p = new projectile()
	{
		dataBlock = RocketLauncherProjectile;
		initialVelocity = %vector;
		initialPosition = %obj.getTransform();
		sourceObject = %obj.getID();
	};
	MissionCleanup.add(%p);
	return %p;
}


Just place a marker --- let's use a "waypoint" marker but you could really use something else, and name it "myMarker". Rotate it to where you want the bullet to go (eg: out the window towards the player) - it's easaier to tell where it's going (down the objects Y axis = GREEN) if you have the editors Object Transforms toggled on. Create a small trigger and line both this and the marker's rotation up so that when the player enters the trigger, a rocket spawns from our marker and heads along the marker's rotation setting, straight for the player.

On the trigger, in "enterCommand", type:
magicBullet(myMarker);


And when triggered by this script, a new rocket projectile will spawn out of thin air, and head along the marker's rotation at the speed of 100, and no doubt freak out our player! Huzzar!

confession: this post was originally a request for help to fix my script, but no sooner had I hit "post" as I realized my faux pas --- so I edited it into a resource style thread. :P