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 /
This question was closed Nov 25, 2014 at 04:20 PM by bobin115 for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by bobin115 · Nov 25, 2014 at 03:34 PM · timeshootingshootcs

simple shooting script help

i am a noob with cs. i have a projectile script that shoots on click, i want to remove that function and add a function that makes it shoot every second. here is the script: using UnityEngine; using System.Collections;

 public class ShootDemo : MonoBehaviour
 {
     public Rigidbody projectile;
     public float speed = 20;
  
   
     void Update ()
     {
         if (Input.GetButtonDown("Fire1")) // this is the part i want to change
         {
             Rigidbody instantiatedProjectile = Instantiate(projectile,transform.position,transform.rotation)as Rigidbody;
             instantiatedProjectile.velocity = transform.TransformDirection(new Vector3(0, 0,speed));
         }
     }
 }

so what i need it for it to shoot WITHOUT CLICKING

any help would be great

thankyou

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

3 Replies

  • Sort: 
avatar image
1
Best Answer

Answer by static_cast · Nov 25, 2014 at 04:08 PM

According to this, I modified it a little bit to fit your needs, so the code would be this:

 using UnityEngine;
 using System.Collections;

 public class ShootDemo : MonoBehaviour
 {
     public Rigidbody projectile;
     public float speed = 20F;
     public float fireRate = 0.5F;
     private float nextFire = 0.0F;

     void Update() {
         if (Time.time > nextFire) {
             nextFire = Time.time + fireRate;
             Rigidbody instantiatedProjectile = Instantiate(projectile,transform.position,transform.rotation)as Rigidbody;
             instantiatedProjectile.velocity = transform.TransformDirection(new Vector3(0, 0,speed));
         }
     }
 }


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 bobin115 · Nov 25, 2014 at 04:14 PM 0
Share

thanks but it has 6 errors and i dont know how to solve them alt text

errors.jpg (126.9 kB)
avatar image static_cast · Nov 25, 2014 at 04:16 PM 0
Share

You forgot to write this:

using UnityEngine;

using System.Collections;

At the VERY top of the program, outside of the class. I updated my code so you can repaste it. ;)

EDIT: $$anonymous$$ake sure to hit that checkmark by my post so people will know that the question has been answered. ;)

avatar image
1

Answer by etaxi341 · Nov 25, 2014 at 03:46 PM

This should work:

 using UnityEngine;
 using System.Collections;

 public class ShootDemo : MonoBehaviour
 {
     public Rigidbody projectile;
     public float speed = 20;
     float timeOver = 0f;
     public float shootEvery = 1f;
     void Update ()
     {
         if (timeOver < shootEvery)
             timeOver += Time.deltaTime;
         else{
             timeOver -= shootEvery;
             Rigidbody instantiatedProjectile = Instantiate(projectile,transform.position,transform.rotation)as Rigidbody;
             instantiatedProjectile.velocity = transform.TransformDirection(new Vector3(0, 0,speed));
         }
     }
 }
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 bobin115 · Nov 25, 2014 at 03:57 PM 0
Share

thanks but not what im after, i think its my fault that i didnt explain it right. i want to us this as an enemy projectile so i just want it to shoot without the need to click. ( and it had a few errors)

avatar image etaxi341 · Nov 25, 2014 at 04:14 PM 0
Share

Then just remove the "Input.GetButton("Fire1") &&" in my Script :)

avatar image bobin115 · Nov 25, 2014 at 04:18 PM 0
Share

i get one error: "(1,26): error CS0246: The type or namespace name `$$anonymous$$onoBehaviour' could not be found. Are you missing a using directive or an assembly reference?"

avatar image etaxi341 · Nov 25, 2014 at 04:21 PM 0
Share

You have to add this at the top:

using UnityEngine; using System.Collections;

But I saw you already got an working answer :) Just to complete my script thats what should be at the top :)

avatar image bobin115 · Nov 25, 2014 at 04:32 PM 0
Share

cheers anyway

avatar image
0

Answer by feyyd · Nov 25, 2014 at 03:57 PM

 float fire_reshoot_track; //defined outside update loop that we use to store when weapon can be fired next
 float interval;           //defined outside update loop

 //inside update loop
 if ( fire_reshoot_track < Time.time ) 
 {
     Fire ();
     fire_reshoot_track = Time.time + interval;
 }


I don't know where I saw this method of tracking refire rates, but I prefer it to other methods I see.

Comment
Add comment · Show 1 · 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 bobin115 · Nov 25, 2014 at 04:02 PM 0
Share

so how would i implement this with my current script (i usually us js not cs)

Follow this Question

Answers Answers and Comments

28 People are following this question.

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

Related Questions

Hey can anyone help with a shooting script??? 0 Answers

Shootin Worminator Like Game 3 Answers

Shoot Script Help 1 Answer

click shooting how do i do it? 1 Answer

Firing System with two Shoot Points 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