Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • 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 Tyler 2 · Nov 16, 2010 at 12:23 AM · movementtimed

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

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 fireDude67 · Nov 16, 2010 at 12:59 AM 0
Share

Please wrap your code with ` (Backqoutes)

3 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Thelo · Nov 16, 2010 at 10:21 AM

  • 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;)

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
avatar image
0

Answer by Proclyon · Nov 16, 2010 at 02:49 PM

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

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
avatar image
0

Answer by ransomink · Jan 17, 2015 at 01:12 PM

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();
     }
 }
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.

Follow this Question

Answers Answers and Comments

1 Person is following this question.

avatar image

Related Questions

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

Switch Case for Basic AI.... 1 Answer

How to make object move and rotate 1 Answer

I am trying to make a movement script for my main camera but it isnt working! 2 Answers

How to make a gameobject shake. 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges