- Home /
OnTrigger and Input.getkey on picking up weapons from the ground.
Hey, I've been trying to make it so I have to click E in order to pick a weapon from the ground. I know that OnTrigger doesnt get called every frame so I tried creating a bool to make it work. However it didnt (other wise i wouldn't be here) and to me, the code makes sense. I'm sure I'm missing out on something, can you help me? Here's the code:
void OnTriggerStay2D(Collider2D other){
if (other.CompareTag("Weapon"))
{
// OnWeaponPickup(other.transform);
weapOnGround = other.transform;
OnWeapTrig = true;
}
else
{
OnWeapTrig = false;
}
}
public void OnWeaponPickup()
{
Transform weapon = weapOnGround;
if(OnWeapTrig == true)
{
if (IsHolsterFull())
{
DestroyWeaponInCurrentSlot();
}
else
{
if (currentWeapon != -1)
{
DisableWeapon(currentWeapon);
}
currentWeapon++;
}
EquipWeaponInCurrentSlot(weapon);
}
}
try adding a rigid body, and ticking the gravity off, sometimes, you'll need to have these on triggers to make them work.
( even with the more common mouse over methods )
I already had a 2drigidbody with gravity = 0 (since its a top down game)
You don't need OnTriggerStay2D to repeatedly set a bool every frame - it would be more efficient to use OnTriggerEnter2D. Please post a screenshot showing your collider/rigidbody settings applied to each object.
I later changed it to Trigger Enter and it didnt work anyways
Use The OnTriggerEnter / Exit. Additionally, you may pass over more than one weapon, or pass over a weapon immediately before a non-weapon.
In the latter case, your code would evaluate OnWeapTrig to false, despite you standing on a weapon.
You might have a few issues in the second statement, but without any extra explanations we can't help too much there:
// $$anonymous$$eep track of the weapons we're in range of
private List<Transform> nearbyWeapons = new List<Transform> ();
// This will be called whenever you enter a trigger collider
void OnTriggerEnter2D (Collider2D other)
{
// Check if this was a weapon
if ( other.CompareTag ("Weapon") )
{
// If so, add it to our list
this.nearbyWeapons.Add (other);
}
}
// This will be called whenever you enter a trigger collider
void OnTriggerExit2D (Collider2D other)
{
// Check if this was a weapon
if ( other.CompareTag ("Weapon") )
{
// Check if we had this previously, and if so remove it
if ( this.nearbyWeapons.Contains (other) )
{
this.nearbyWeapons.Remove (other);
}
}
}
// Your pickup function
public void OnWeaponPickup ()
{
// $$anonymous$$ake sure we were standing on something
if ( this.nearbyWeapons.Count == 0 )
return;
// You can choose what to do here for deter$$anonymous$$ing which weapon to use
Transform weapon = this.nearbyWeapons [0];
// Possible issue? If your weapon is full, it looks like you are
// trying to remove the current one, but then this statement will
// do nothing else following that?
//
if ( IsHolsterFull () )
{
DestroyWeaponInCurrentSlot ();
}
else
{
if ( currentWeapon != -1 )
{
DisableWeapon (currentWeapon);
}
currentWeapon++;
}
EquipWeaponInCurrentSlot (weapon);
}
Everything is working fine, I'm just having dificulties doing it so only if i press e will the OnWeaponPickUp get called
To capture input, you can use Update and the Input.Get$$anonymous$$eyDown function:
void Update ()
{
if ( Input.Get$$anonymous$$eyDown ($$anonymous$$eyCode.E) )
{
this.OnWeaponPickup ();
}
}
Answer by simone9725 · Jul 24, 2017 at 05:12 PM
Why do you use on trigger stay ,the collision I guess happens one time and then destroy or do other stuff. You can change this:
void OnTriggerStay2D(Collider2D other) to void OnTriggerEnter2D(Collider2D other)
Answer by shadowpuppet · Jul 24, 2017 at 05:29 PM
I have several scripts that also use the E key to do stuff. i use onTriggerStay with Getkey because you only enter the trigger once and would need to be pressing the E key as you enter while you stay there and press E something happens.So the condition of being in trigger zone and pressing E is what this script does. The script goes on the triggerzone
void OnTriggerStay (Collider other) {
if(other.tag == "Player")
{
if (Input.GetKeyDown(KeyCode.E))
{
//something happens
}
}
}
Unless the physics updates and UI updates are synced, the centermost If statement isn't going to execute reliably.