x


GetKeyUp problem

I've been trying to make a script to pick up weapons and I'm just testing it to see if all the triggers work and whatnot and the GetKeyUp isn't working.

Heres the script on the player:

using UnityEngine;
using System.Collections;


public class Player : MonoBehaviour {
    public bool equip;

    // Use this for initialization
    void Start () {

    }

    // Update is called once per frame
    void Update () {

    }

    void OnTriggerEnter(Collider other){

       if (other.gameObject.tag == "weapon"){
         if(Input.GetKeyUp("e")){
          equip = true;
          other.gameObject.SendMessage("Equip");

         }
       }

    }


}

and the weapons:

using UnityEngine;
using System.Collections;

public class Weapon : MonoBehaviour {

    // Use this for initialization
    void Start () {

    }

    // Update is called once per frame
    void Update () {

    }


    void Equip(){
       print("picked up weapon");
    }
}

I know the triggers work because I tested them but when I added the next if GetKeyUp it doesn't perform either of those functions. Equip stays false and I get no picked up weapon message in the console. But I also get no error messages. Can anyone see what I am doing wrong?

more ▼

asked May 03 '12 at 06:10 PM

cidmodder gravatar image

cidmodder
191 47 56 69

Are you sure your collider is set to isTrigger? And are you sure your object is tagged "weapon".

May 03 '12 at 06:21 PM OrangeLightning
(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

@cidmodder You would have to put it in a OnTriggerStay function, right now you would have to release the mouse button the same frame the trigger gets triggered. Change that to stay and you should be good to go

void OnTriggerStay(Collider other)
{
   if (other.gameObject.tag == "weapon")
   {
       if(Input.GetKeyUp("e"))
       {
           equip = true;
           other.gameObject.SendMessage("Equip");
       }
   }
}
more ▼

answered May 03 '12 at 06:33 PM

hijinxbassist gravatar image

hijinxbassist
2k 23 31 38

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

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

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x4155
x1949
x8
x1

asked: May 03 '12 at 06:10 PM

Seen: 442 times

Last Updated: May 03 '12 at 08:55 PM