Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice
  4. Dismiss Notice

Unexpected delay on mouse input

Discussion in 'Scripting' started by thisiswater, May 3, 2016.

  1. thisiswater

    thisiswater

    Joined:
    Apr 28, 2016
    Posts:
    5
    EDIT:
    I've realized this is system wide after pressed any key - I've not used this laptop for anything where I'd notice this. Not a unity problem, I'll find the answer somewhere.


    Hi guys,

    I'm writing a script to cast spells.
    The script switches between casting mode and non-casting mode, creates a sprite which follows the mouse if casting mode is active and destroys the sprite if casting mode is left, and casts a spell if the mouse is clicked while casting mode is active (casting the spell also leaves casting mode).

    All seems to be working fine, except after entering casting mode there is a delay of 1-2 seconds before the script will register a mouse click. There is no problem clicking outside of casting mode (as reported by debug.log).

    What could be causing this problem? Is it an efficiency problem of some kind?

    Code is below. Thanks!

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class AbilityUsage : MonoBehaviour {
    5.  
    6.     private bool casting;
    7.     public GameObject castingsprite;
    8.     public GameObject fireball;
    9.     private GameObject castingspriteinstance;
    10.  
    11.  
    12.     // Use this for initialization
    13.     void Start () {
    14.  
    15.         casting = false;
    16.  
    17.  
    18.     }
    19.  
    20.     // Update is called once per frame
    21.     void Update () {
    22.  
    23.         if (Input.GetKeyDown ("q"))
    24.         {
    25.  
    26.             casting = !casting;
    27.             Debug.Log (casting);
    28.  
    29.             if (casting) {
    30.  
    31.                 castingspriteinstance = Instantiate (castingsprite, transform.position, transform.rotation) as GameObject;
    32.                  
    33.             }     else
    34.             {
    35.                 Destroy (castingspriteinstance);
    36.             }
    37.         }
    38.  
    39.         if (casting) {
    40.             Vector3 castingspriteloc;
    41.  
    42.             castingspriteloc = Camera.main.ScreenToWorldPoint (Input.mousePosition);
    43.             castingspriteloc = new Vector3 (castingspriteloc.x, castingspriteloc.y, 0);
    44.             castingspriteinstance.transform.position = castingspriteloc;
    45.         }
    46.      
    47.         if (Input.GetMouseButtonDown (0))
    48.         {
    49.  
    50.             Debug.Log ("got the click");
    51.  
    52.             if (casting) {
    53.                 Debug.Log ("You are casting AND clicked. Have I cast?");
    54.                 GameObject fireballinstance;
    55.                 fireballinstance = Instantiate (fireball, transform.position, transform.rotation) as GameObject;
    56.                 Destroy (castingspriteinstance);
    57.                 casting = false;
    58.             }
    59.         }
    60.     }
    61.  
    62. }
    63.  
     
    Last edited: May 3, 2016
  2. thisiswater

    thisiswater

    Joined:
    Apr 28, 2016
    Posts:
    5
    According to a timer I've rigged in, it takes around 0.85 seconds to register the click after casting, if I'm spamming the mouse button.