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 Bruno-Fludzinski · Jun 24, 2012 at 04:31 PM · turretwaitseconds

Wait five seconds

I'm making a script that when my player crosses a line a turret points and shoots at me I wrote all of that but now I want it to look at me , wait five seconds then shoot at me I tried looking at unity script reference but the stuff they give you can't be used in function Update() here is my script:

 var target : Transform;
 var rotationspeed : float = 30;
 var fireRate : float = 0.1;
 var Prefab: Transform;
 private var nextFire = 2.0;
 var speed : float = 50;
 function Update(){
 transform.Rotate(Vector3.up * Time.deltaTime * rotationspeed);
 if(target.position.z>109){
 transform.LookAt(target);
 //here is where I want it to wait 5 seconds
 nextFire = Time.time + fireRate;
     var copy = Instantiate(Prefab,GameObject.Find("Mini Gun Spawn").transform.position,transform.rotation);
    copy.rigidbody.velocity = transform.TransformDirection(Vector3.forward * speed);
     
 }
 }
Comment
Add comment
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

2 Replies

· Add your reply
  • Sort: 
avatar image
2
Best Answer

Answer by aldonaletto · Jun 25, 2012 at 04:41 AM

There's a simple way to do that with your current script: keep setting nextFire to Time.time+5 while the target is behind the shot range - this way there will be a 5 seconds delay when the player enters the shot range, and after that the shots will obey the fireRate interval:

function Update(){
  transform.Rotate(Vector3.up * Time.deltaTime * rotationspeed);
  if(target.position.z= nextFire) { // and shoot when nextFire time reached:
       nextFire = Time.time + fireRate; // set new nextFire time...
       var copy = Instantiate(Prefab,GameObject.Find("Mini Gun Spawn").transform.position,transform.rotation); // create projectile...
       copy.rigidbody.velocity = transform.TransformDirection(Vector3.forward * speed); // and shoot it
    }
  }
}
Comment
Add comment · Show 2 · 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 Bruno-Fludzinski · Jun 25, 2012 at 03:23 PM 0
Share

now it just shoots one bullet every five seconds. i want it to wait five secondsand then rapidly shoot.

avatar image Mizuho · Jun 27, 2012 at 05:21 AM 0
Share

@bfludzinski: Here's the copy that does it...

 function Update(){
   transform.Rotate(Vector3.up * Time.deltaTime * rotationspeed);
   if(target.position.z<=109){ // while player outside shot     range...
     nextFire = Time.time + 5; // set nextFire to 5 seconds from now
   } else { // but when inside range...
     transform.LookAt(target); // look to the player...
     if (Time.time >= nextFire) { // and shoot when nextFire time reached:
        if(target.position.z <= 109) nextFire = Time.time + fireRate; // set new nextFire time...
        var copy = Instantiate(Prefab,GameObject.Find("$$anonymous$$ini Gun Spawn").transform.position,transform.rotation); // create projectile...
        copy.rigidbody.velocity = transform.TransformDirection(Vector3.forward * speed); // and shoot it
     }
   }
 }

Added:

 if(target.position.z <= 109)

In front of the nextFire setting, so it only resets the time when the player is out of range again.

Note that this is actually @aldonaletto's code, so if it works for you, just check his as correct.

avatar image
0

Answer by whydoidoit · Jun 24, 2012 at 04:56 PM

You can't use Coroutines inside update but you can just start one using StartCoroutine from update:

   function Update() {
       if(somethingHappened) {
           StartCoroutine("ShootAtMe");
       }
   }

   function ShootAtMe() {
        yield new WaitForSeconds(5);
        //Start shooting
   }
Comment
Add comment · Show 4 · 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 Bruno-Fludzinski · Jun 25, 2012 at 04:01 AM 0
Share

heres my script:

var target : Transform; var rotationspeed : float = 30; var fireRate : float = 0.1; var Prefab: Transform; private var nextFire = 2.0; var speed : float = 50; function Update(){ transform.Rotate(Vector3.up Time.deltaTime rotationspeed); if(target.position.z>109){ transform.LookAt(target); //here is where I want it to wait 5 seconds nextFire = Time.time + fireRate; var copy = Instantiate(Prefab,GameObject.Find("$$anonymous$$ini Gun Spawn").transform.position,transform.rotation); copy.rigidbody.velocity = transform.TransformDirection(Vector3.forward * speed);

} }

avatar image whydoidoit · Jun 25, 2012 at 03:28 PM 0
Share

Could you try and post that code formatted (indent it in the edtior by 4 spaces or one tab before pasting) - I can't really seem to format it right :)

We need to put the transform.LookAt(target) etc etc after your comment in the //Start shooting part of my answer and all of the stuff before that in the update - replacing somethingHappened in the first part of my answer with the target.position.z > 109

avatar image AlucardJay · Jun 25, 2012 at 03:34 PM 0
Share

or ... in monodevelop, highlight all your code, then hit tab then paste that =]

avatar image Bruno-Fludzinski · Jun 27, 2012 at 05:16 AM 0
Share
 var target : Transform;
 var rotationspeed : float = 30;
 var fireRate : float = 0.1;
 var Prefab: Transform;
 private var nextFire = 2.0;
 var speed : float = 50;
 function Update(){
 transform.Rotate(Vector3.up * Time.deltaTime * rotationspeed);
 if(target.position.z>109){
 transform.LookAt(target);
 //here is where I want it to wait 5 seconds
 nextFire = Time.time + fireRate;
     var copy = Instantiate(Prefab,GameObject.Find("$$anonymous$$ini Gun Spawn").transform.position,transform.rotation);
    copy.rigidbody.velocity = transform.TransformDirection(Vector3.forward * speed);
 
 }
 }

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

6 People are following this question.

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

Related Questions

Load Level delay on collision C# 1 Answer

WaitForSeconds Problem [C#] 4 Answers

yield new wait for seconds: what does "new" do? 1 Answer

How do I play simultaneous sounds one at a time? 2 Answers

C# wait X-Seconds in Code before continue in Code? 3 Answers


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