- Home /
Slow automatic shooting script?
Hi, i am having a problem, i currently have the script below that shoots a projectile (it is semi-automatic). i know how to change it to automatic, just change the GetButtonDown to GetButton, but that it too fast for what i want, please can you help me change the fire rate (preferably using a variable) so that it shoots automaticly but for the fire rate to be around 0.5. thanks
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 );
}
}
I just like to add that
transform.TransformDirection( Vector3( 0, 0, speed ) )
can be replaced with
transform.forward * speed
Answer by BiG · Apr 18, 2012 at 07:32 PM
I've modified your script. Now, the variable "rate" controls the amount of time between a projectile and another (0.5 seconds, in the declaration).
var projectile : Rigidbody;
var speed = 20;
var rate : float = 0.5;
private var rate_time : float;
function Update()
{
if( Input.GetButtonDown("Fire1") && Time.time > rate_time)
{
rate_time = Time.time + rate;
var instantiatedProjectile : Rigidbody = Instantiate(projectile, transform.position, transform.rotation );
instantiatedProjectile.velocity = transform.TransformDirection( Vector3( 0, 0, speed ) );
Physics.IgnoreCollision( instantiatedProjectile. collider, transform.root.collider );
}
}
Thanks a lot, that really helped! you made one error though (GetButtonDown should have been GetButton) but that doesnt matter as it is fixed now thanks
Also, how am i able to convert this script so it can be used on a turret (shoots automaticaly)?
Something like that:
var projectile : Rigidbody;
var speed = 20;
function Start()
{
while (true)
{
yield WaitForSeconds(1);
//flag = Random.Range(0,3);
//if (flag==1){
var instantiatedProjectile : Rigidbody = Instantiate(projectile, transform.position, transform.rotation );
instantiatedProjectile.velocity = transform.TransformDirection( Vector3( 0, 0, speed ) );
Physics.IgnoreCollision( instantiatedProjectile. collider, transform.root.collider );
//}
}
}
The commented lines, if not commented, implement a "more realistic" turret, that doesn't shoot programmatically at every second.
sorry but this script doesnt work for me (it only fires once, then stops), What i wanted was a script that shoots automaticaly (e.g. i have placed the turret on the map that looks at the player, and have added and empty gameobject at the end of the barrel) i want to use the script above that you modified for me(the first one), but take away the GetButtonDown part, so it just shoots.
thanks again
Strange. It should work, at least for the logic. By the way, I didn't understood if you solved "recycling" the first script (it seems a good idea as well, in a first analysis...). Let me know...
Answer by dannyskim · Apr 18, 2012 at 07:18 PM
Coroutines are what you probably want to use for this case:
http://unity3d.com/support/documentation/ScriptReference/Coroutine.html
Your answer
Follow this Question
Related Questions
Projectile Script 1 Answer
Object reference not set to an instance of an object 1 Answer
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
shoot script is not working 1 Answer