can someone please take a look at this movement script?

Hello, I have a long question but I while try to be as concise as I can. I am making a 2D Shooter (like geometry wars, super stardust HD, etc.). I have used a script to make the player object rotate/look in a direction depending on the mouse cursor position relative to the camera (so if the mouse cursor is above the object on the screen it is facing up, and if a move the mouse clockwise, the object will rotate clockwise). I have attached the following script to the object that rotates,

var projectile : Rigidbody; 
var speed = 20; 
function Update() 
{ 
 if( Input.GetButtonDown( "Fire1" ) ) 
 { 
  var instantiatedProjectile : Rigidbody = Instantiate( 
   projectile, transform.position, transform.rotation ); 
  instantiatedProjectile.velocity = 
   transform.TransformDirection( Vector3( 0, 0, speed ) ); 
  Physics.IgnoreCollision( instantiatedProjectile. collider, 
   transform.root.collider ); 
 } 
}

It makes it so that when I click the mouse, a projectile moves forwards(up along with the default view) from the object that rotates. But, since I used another script to rotate the object, the projectile moves towards the mouse (because the mouse direction is now its forward direction). I want to modify the script (the one posted above) to do either of the following

1:Fire every .5 second, regardless of whether the mouse is held down or not. 2:Fire every .5 second when the mouse is held down (not just pressed once, but held down).

I have tried instantiate scripts and stuff like that, but they seem ignoring the rotation of the object. Thank you for reading my long post. :)

  • Have an instance variable that keeps the time of the last fired shot, say "lastFiredShotTime". Initialize it to -10 .

  • Change the firing check so it only fires if at least 0.5 seconds have elapsed since the last shot, and use GetButton() instead of GetButtonDown(). In other words, go from

`if( Input.GetButtonDown( "Fire1" ) )`

to something like

`if( Time.time - lastFiredShotTime > 0.5 && Input.GetButton( "Fire1" ) )`

  • When you fire a bullet, set lastFiredShotTime to the current time (`lastFiredShotTime = Time.time;`)

Detecting an input that is HELD DOWN Lesson 101 in a proper sample ( edited again)

//If an A or B key was pressed last frame
if (!A_Old || !B_Old)
{
    //If either A or B key was pressed this frame (Both = held down)
    if (A_New || B_New)
}

remember to not forget that the last frame needs to be updated and that the release can be simlarly overlooked creating sticky buttons

First question:
You can create two variables: one which controls the rate of fire (rateOfFire), and the other holds the time of the last shot fired (lastFired).

public var rateOfFire : float;	// Rate of fire for the projectile 
private var lastFired : float;	// Time of the last shot fired

Instead of placing all of your code inside the Update() function, you should move it into a Shoot() function, like so…

function Shoot () 
{
		// Create the projectile
		var instantiatedProjectile : Rigidbody = Instantiate( projectile, transform.position, transform.rotation ); 
		
		// Propel the projectile forward based on the gameobject's current direction
		instantiatedProjectile.velocity = transform.TransformDirection( Vector3( 0, 0, speed ) ); 
		
		// Ignore collisions from the projectile and the gameobject
		Physics.IgnoreCollision( instantiatedProjectile. collider, transform.root.collider );
}

Now, in order to fire a projectile every (n) seconds, you need to check if the current time subtracted from the last fired shot is past the rate at which to fire. Simply add an if statement and place your instantiate and fire code inside…

function Shoot () 
{
	// If the current time is past the rate of fire ...
	if ( Time.time - lastFired >= rateOfFire )
	{
		// ... create and fire a projectile
		
		// Create the projectile
		var instantiatedProjectile : Rigidbody = Instantiate( projectile, transform.position, transform.rotation ); 
		
		// Propel the projectile forward based on the gameobject's current direction
		instantiatedProjectile.velocity = transform.TransformDirection( Vector3( 0, 0, speed ) ); 
		
		// Ignore collisions from the projectile and the gameobject
		Physics.IgnoreCollision( instantiatedProjectile. collider, transform.root.collider );
	}

Then all you need is to place the Shoot() function inside Update().

function Update ()
{
	// Shoot a projectile
	Shoot();
}

Second question: The only difference between the first question is in the Update() function. Check if the Fire1 button was pressed.

function Update ()
{
	// If the fire button is pressed ...
	if ( Input.GetButtonDown( "Fire1" ) )
	{
		// ... shoot a projectile
		Shoot();
	}
}