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
1
Question by jjplay175 · Jun 12, 2015 at 12:13 PM · raycastupdate

Script to only run once on click?

So I have a system where 2 scripts control all interactions, I have my mouse script that uses a raycast to get the GameObject it collides with name and if it is an InteractionBox then it enabled the InteractionInfo script which handles it all

In Interaction there are a few booleans that tell it what scripts are in its parents so it knows what to do once it is activated

The problem is... the way that it works means that when I click the interaction box on a spawner for example it will spawn 2-3 times instead of just once?

 public class NMouseController : MonoBehaviour {
 
     public float LookAtSpeed;
 
     public bool Firing;
     private InteractionInfo interactionInfo;
 
 
     void Start () {
     }
 
     void FixedUpdate () {
 
         if (Firing == false) {
             Invoke("LookAtMouse", 0);
         }
 
         if (Input.GetButton ("CommandKey") && Input.GetButton ("FireKey")) {
             Firing = true;
             Invoke("TargetDetails", 0);
         }
 
         if (!Input.GetButton ("CommandKey") && Input.GetButton ("FireKey")) {
             Invoke("ClickDetect", 0);
         }
 
     }
 
     void ClickDetect(){
         Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition);
         Debug.DrawRay(ray.origin, ray.direction * 100, Color.yellow, 10);
         RaycastHit hit;
         if (Physics.Raycast (ray, out hit)) {
             string HitObj = (hit.transform.gameObject.name);
             if (HitObj == "InteractionBox"){
                 interactionInfo = hit.transform.gameObject.GetComponent<InteractionInfo>();
                 interactionInfo.enabled = true;
             }
         }
     }

And this is my Interaction script that is called

 public class InteractionInfo : MonoBehaviour {
 
     public Vector3 RayHitPos;
 
     public bool SpawnScript;
     public bool EnemyAIScript;
     public bool MovementScript;
     private SpawnButton spawnButton;
 
     private GameObject GameObj;
 
     void Start(){
         GameObj = transform.parent.gameObject;
         this.enabled = false;
     }
 
     void Update(){
 
         if (SpawnScript == true) {
             Invoke ("ActivateSpawn", 0);
         }
     }
 
 
     void ActivateSpawn(){
         spawnButton = GameObj.GetComponent<SpawnButton>();
         spawnButton.Spawn = true;
         this.enabled = false;
     }
 }

I assume I would go about using Time.time somehow to stop it registering 1 click in 2-3 frames

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

1 Reply

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

Answer by Dave-Carlile · Jun 12, 2015 at 01:00 PM

A couple of things:

  • Input.GetButton Returns true when the button is held down, so if it's held down across multiple frames it'll return true each time, so you'll get the multiple presses behavior. If you want to just see a single press until it's released and pressed again you want to use Input.GetButtonDown.

  • You don't want to use input functions inside of FixedUpdate because the input state is reset each frame to deal with things like "button was pressed this frame". You'll see intermittent input oddities if you do it in FixedUpdate. So move all of your input handling into Update. Here's a discussion on it.

If you are actually wanting the "repeat fire while button held" thing, then you're right, you'll need to use some sort of timer to space out the shots, otherwise you'll get one once per frame. Each time you fire a shot, set a timer to 0, each update add Time.deltaTime to the timer, and once it reaches your repeat threshold, fire another shot, reset the time to 0.

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 jjplay175 · Jun 13, 2015 at 01:48 PM 0
Share

Shooting script is seperate and uses a FireRate variable time in the if statement

Anyway yeh I see the mistake I made there and changed it to update and changed to input.getbuttondown Thanks for the help!

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

22 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

Related Questions

Raycast stops when false, even in update() 1 Answer

Mouse Input for RayCast - Update or FixedUpdate 1 Answer

Only a portion of update gets called? 1 Answer

Click or touch getting detected multiple times 1 Answer

Why is my cursor not moving with the updating position of the raycast hit point? 0 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