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. Dismiss Notice

Input keyboard or input gamepad never fired

Discussion in 'Scripting' started by arrezes, Sep 28, 2016.

  1. arrezes

    arrezes

    Joined:
    Jul 12, 2014
    Posts:
    7
    Hi,

    I am experiencing strange things with my code. I am making a simple ARVR first-person shooter game using Vuforia and Google Cardboard.

    After detecting my image target, my game starts to instantiate game objects. Below is the lines of code in the script I attached to my Image Target

    Code (CSharp):
    1. private void OnTrackingFound()
    2.     {              
    3.         Debug.Log("Image Target Found!");
    4.         myPrefab = Instantiate(glowtab, Vector3.zero, Quaternion.identity) as GameObject;
    5.         myPrefab.transform.parent = mTrackableBehaviour.transform;      
    6.         myPrefab.transform.localRotation = Quaternion.AngleAxis(90, new Vector3(1, 0, 0));
    7.         myPrefab.transform.localPosition = new Vector3(0.0f, 0.0f, 0.0f);
    8.         myPrefab.SetActive(true);
    9. InvokeRepeating("SpawnAgents", 3.0f, 2.0f);
    10. }
    By invoking the SpawnAgents game starts instantiating randomly agents as the child of image target.
    Code (CSharp):
    1. public void SpawnAgents()
    2.     {
    3.         myAgent = Instantiate(agent, Vector3.zero, Quaternion.identity) as GameObject;
    4.         myAgent.GetComponent<AgentBehaviour>().SpawnAgents(mTrackableBehaviour);
    5.  
    6.     }
    I also attach a script to manage the agent, the script names "AgentBehaviour". The agent also will be randomly assigned material to it. Since it is a VR game I used gaze input module and GvrReticle to gaze to a target. This is where my problem starts. Upon entering the OnPointerEnter method pressing designated Key on the keyboard and even on the gamepad just a waste because it never fired. to test it I added lines of code to see what happened if the game does not accept keyboard and gamepad input. Below is the code in the AgentBehaviour script
    Code (CSharp):
    1. public class AgentBehaviour : MonoBehaviour, IPointerEnterHandler {
    2.    
    3.     enum testMat { Fire1, Fire2, Fire3, Fire4, Alma, Balut};
    4.     Renderer rend;
    5.     public GameObject prefabsExplosion;
    6.     private GameObject explosion;
    7.  
    8. public void SpawnAgents(TrackableBehaviour imageTarget)
    9.     {
    10.        
    11.         rend = this.GetComponent<Renderer>();
    12.         rend.enabled = true;
    13.        
    14.         this.transform.parent = imageTarget.transform;
    15.         this.transform.localScale = new Vector3(0.1f, 0.1f, 0.1f);
    16.         this.transform.localPosition = new Vector3(UnityEngine.Random.Range(-0.5f, 0.5f), 0.2f, UnityEngine.Random.Range(-0.5f, 0.5f));
    17.         this.transform.localRotation = Quaternion.identity;
    18. Material mat;
    19.         int n;
    20.  
    21.         n = (int)UnityEngine.Random.Range(0, 5);
    22.         Debug.Log("Random n =" + n);
    23.  
    24.         mat = Resources.Load(Enum.GetName(typeof(testMat), n), typeof(Material)) as Material;
    25.         rend.material = mat;
    26.      
    27.     }
    28. public void OnPointerEnter(PointerEventData eventData)
    29.     {
    30.         Debug.Log("OnPointerEnter triggered");
    31.  
    32.         GameObject agentToDestroy;
    33.  
    34.         if (Input.GetButtonDown("Fire1"))   //never fired
    35.         {
    36.             Debug.Log("Ready to explode!");
    37.            
    38.             Invoke("SetExplosion", 1.0f);          
    39.             agentToDestroy = ExecuteEvents.GetEventHandler<IPointerEnterHandler>(eventData.pointerEnter);
    40.             Destroy(agentToDestroy);
    41.         }      
    42.         else if (Input.GetKeyDown(KeyCode.Space)) //never fired
    43.         {
    44.             Debug.Log("Ready to explode!");          
    45.             Invoke("SetExplosion", 1.0f);          
    46.             agentToDestroy = ExecuteEvents.GetEventHandler<IPointerEnterHandler>(eventData.pointerEnter);
    47.             Destroy(agentToDestroy);
    48.         }
    49.         else
    50.         {
    51.             Debug.Log("Break free!!");            
    52.             Invoke("SetExplosion", 1.0f);
    53.             agentToDestroy = ExecuteEvents.GetEventHandler<IPointerEnterHandler>(eventData.pointerEnter);
    54.             var materialName = agentToDestroy.GetComponent<Renderer>().material.name;
    55.             //var material1Name = agentToDestroy.GetComponent<Material>().name; if this line added Invoke("SetExplosion") fired. if this line removed the Invoke("SetExplosion") never fired but the object agentToDestroy successfully destroyed.
    56.             Debug.Log(materialName.ToString());
    57.             Destroy(agentToDestroy);          
    58.         }
    59.  
    60. }
    61.  
    62.     void SetExplosion()
    63.     {
    64.         Debug.Log("Exploded!!");
    65.         prefabsExplosion = Resources.Load<GameObject>("Explosion");
    66.         explosion = Instantiate(prefabsExplosion, Vector3.zero, Quaternion.identity) as GameObject;
    67.         explosion.transform.parent = this.transform;
    68.         explosion.transform.localPosition = new Vector3(0.0f, 0.0f, 0.0f);
    69.        
    70.     }
    71.   }
    the line I commented is where the problems lie. Very strange and I don't know where can I fix to solve the problem. What I want to achieve is I am able to Destroy the gazed gameobject upon pressing gamepad Fire1 button and set the explosion off on that particular gameobject.

    Hopefully somebody can find what is wrong with my code.
     
  2. steego

    steego

    Joined:
    Jul 15, 2010
    Posts:
    967
    I think your problem is that you are using Input.GetButtonDown, this only returns true the exact frame that the button was pressed, which is unlikely to happen at the same time as OnPointerEnter.

    The way to solve this in your case would probably be something like this: In your OnPointerEnter method, only set the current target. Use OnPointerExit to clear the target if needed. Then check for GetButtonDown in your Update method, and if an input event is detected there, do stuff with the current target that you set.
     
    arrezes likes this.
  3. arrezes

    arrezes

    Joined:
    Jul 12, 2014
    Posts:
    7
    Thank you...I am very satisfied with the current situation after following your advice, With some minor tweak like adding spawn point empty game object to represent location for the explosion. I am more than happy to see the result, just for your info the agent is destroyed after the pressing the button, however I can see multiple places of explosion spawned. I reckoned, this could be due to instances of agents spawned on the same frame.