x


Timed events (that repeat)?

Would it be possible to have something like this: http://newgrounds.com/portal/view/417593 where the projectiles are shot at a set time interval (for example: every half second) at a set velocity without the need for mouse input?

more ▼

asked Nov 15 '10 at 02:49 AM

Tyler 2 gravatar image

Tyler 2
1.1k 212 247 264

(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

Absolutely! Take a look at InvokeRepeating. You can use it to repeatedly call the specified function at a given interval.

The function that you call could spawn the projectile with a default velocity.


The script you've posted in the comments seems to be a bit of frankencode, but from it I think I can see what you're trying to do with it.

I think this is closer to what you want:

var projectile : Rigidbody;
var projectileSpeed : float = 10;

function Start ()
{
   InvokeRepeating("LaunchProjectile", 2, 0.3);
}

function LaunchProjectile ()
{
   var instance : Rigidbody = Instantiate(projectile, transform.position, transform.rotation);
   instance.velocity = transform.forward * projectileSpeed; // usually use AddForce
   Physics.IgnoreCollision( instance.collider, transform.root.collider );
}

If you don't understand any particular part, check out the Scripting Reference or comment back!

more ▼

answered Nov 15 '10 at 03:16 AM

Marowi gravatar image

Marowi
4.9k 4 14 53

Thank you. When I tried that script and selected a projectile, after that projectile was shot, the next one would be shot originating from the first projectile, and then the third projectile was shot from the second, etc. How can I make it so that it always shoots from the player (I added the script to the player object)?

Nov 15 '10 at 03:41 AM Tyler 2

If the script is on the player object, use 'transform.position' to get the current position of the player. If you're having troubles, update your question with your code and I can give you a more specific answer!

Nov 15 '10 at 06:54 AM Marowi

// Starting in 2 seconds. // a projectile will be launched every 0.3 seconds

var projectile : Rigidbody;

InvokeRepeating("LaunchProjectile", 2, 0.3);

function LaunchProjectile () { var instance : Rigidbody = Instantiate(projectile); instance.velocity = transform.position; transform.rotation ; instantiatedProjectile.velocity = transform.TransformDirection( Vector3( 0, 0, speed ) ); Physics.IgnoreCollision( instantiatedProjectile. collider, transform.root.collider ); }

This script is the one you linked me to spliced with another script that fires when I click down.

Nov 16 '10 at 12:21 AM Tyler 2

I think you're missing some of your code. 'transform.rotation' isn't being set, and theirs no definition for 'instantiatedProjectile'.

Nov 16 '10 at 01:48 AM Marowi

I've updated my answer with some example code based on yours.

Nov 16 '10 at 01:52 AM Marowi
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x326
x27
x15

asked: Nov 15 '10 at 02:49 AM

Seen: 2234 times

Last Updated: Nov 15 '10 at 02:49 AM