• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by OllieParkes · Apr 18, 2012 at 07:09 PM · instantiateshootingprojectileautomatic

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 );
 }
 }
Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Bunny83 · Apr 19, 2012 at 10:48 AM 0
Share

I just like to add that

 transform.TransformDirection( Vector3( 0, 0, speed ) )

can be replaced with

 transform.forward * speed

2 Replies

· Add your reply
  • Sort: 
avatar image
1

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 );
    }
 }
Comment
Add comment · Show 5 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image OllieParkes · Apr 18, 2012 at 07:58 PM 0
Share

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

avatar image OllieParkes · Apr 18, 2012 at 08:06 PM 0
Share

Also, how am i able to convert this script so it can be used on a turret (shoots automaticaly)?

avatar image BiG · Apr 18, 2012 at 08:13 PM 0
Share

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.

avatar image OllieParkes · Apr 18, 2012 at 08:23 PM 0
Share

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

avatar image BiG · Apr 19, 2012 at 09:52 AM 0
Share

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...

avatar image
0

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

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

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

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

7 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to make projectiles shoot diagonally? 1 Answer

Object reference not set to an instance of an object 1 Answer

projectile limitation problems 1 Answer

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

shoot script is not working 1 Answer

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges