How to check if two conditions have been met? - c#

I am trying to have my player walk onto a GameObject and if they are on that object and they press the space key show a debug log.
private void OnTriggerEnter2D(Collider2D other)
{
if (other.gameObject.tag == "Level_1" && Input.GetKeyDown(KeyCode.Space))
{
Debug.Log("Both Conditions Reached");
}
}

This would only trigger if they were holding down the space bar as they entered the object. You would do better to check if they were currently colliding with the object when the spacebar is pressed. (or do the check for both in Update)
Call this in your player object:
private void OnTriggerEnter2D(Collider2D other) {
if (other.gameObject.tag == "Level_1") {
player.isInside = true;
}
}
private void OnTriggerExit2D(Collider2D other) {
if (other.gameObject.tag == "Level_1") {
player.isInside = false;
}
}
And use this to check for the spacebar:
public void Update() {
if (Input.GetKeyDown(KeyCode.Space) && player.isInside == true) {
Debug.Log("Both Conditions Reached");
}
}

Related

Input not registering in OnCollisonEnter2D,OnCollisionExit2D and OnCollisionStay2D

I am trying to get input when my player collides with another object. But the problem is i am not getting any input. I am getting inputs in update function but not in OnCollisonEnter2D,OnCollisionExit2D and OnCollisionStay2D.
Here is my script
public class PlayerInteractions : MonoBehaviour {
[SerializeField] private GameObject chestHelperMessage;
private void OnCollisionEnter2D(Collision2D collision) {
if (collision.gameObject.CompareTag("Chest")) {
chestHelperMessage.SetActive(true);
if (Input.GetKeyDown(KeyCode.E)) {
Debug.Log("You got a weapon");
}
}
}
private void OnCollisionExit2D(Collision2D collision) {
if (collision.gameObject.CompareTag("Chest")) {
chestHelperMessage.SetActive(false);
}
}
private void OnCollisionStay2D(Collision2D collision) {
if (collision.gameObject.CompareTag("Chest")) {
if (Input.GetKeyDown(KeyCode.E)) {
Debug.Log("Chest opened");
}
}
}
}
none of the function is giving inputs.
Please help.
Thanks
for OnCollisonEnter2D and OnCollisionExit it is pretty unlikely that you press the key down exactly in the very same frame where the collision event happens.
Also for OnCollisionStay2D the call happens together with FixedUpdate, the physics routine. They are not happening every frame so it can happen that also here you miss the one or other input.
In general you should get user input in Update and do e.g.
// In case you need the chest reference itself
private GameObject currentChest;
IEnumerator CheckInputRoutine()
{
if (chestHelperMessage.activeSelf && Input.GetKeyDown(KeyCode.E))
// Or also
//if(currentChest && Input.GetKeyDown(KeyCode.E))
{
Debug.Log("You got a weapon");
}
}
private void OnCollisionEnter2D(Collision2D collision)
{
if (collision.gameObject.CompareTag("Chest"))
{
chestHelperMessage.SetActive(true);
currentChest = collision.gameObject;
StartCorouine (CheckInputRoutine());
}
}
private void OnCollisionExit2D(Collision2D collision)
{
if (collision.gameObject.CompareTag("Chest"))
{
chestHelperMessage.SetActive(false);
currentChest = null;
StopAllCoroutines ();
}
}
This is a piece of code from my game that works for me:
void OnTriggerEnter(Collider other) // you can check if the collider is a player but I did not in this instance
{
HelperMessage.SetActive(true);
canOpenChest = true;
}
void Update()
{
if (canOpenChest && Input.GetKeyDown(KeyCode.E))
{
HelperMessage.SetActive(false); // set active to false because the chest is now open
Destroy(gameObject); // instead of this play an animation or whatever and, you can also remove the collider from the chest
}
}
void OnTriggerExit(Collider other)
{
Text.SetActive(false);
canPickup = false;
}`
The answer by derHugo is technically right but I don't like IEnumerators so I never use them
Thanks #RoberStan, the code given by you is not working for my game, but some changes made it work.
Here is the new code
// text on the screen to help the player which key to press to pick up the item
[SerializeField] private GameObject chestHelperMessage;
private GameObject currentChest;
private void CheckInput() {
// checking that if the helperMessage is active and getting key inputs
if (chestHelperMessage.activeSelf && Input.GetKeyDown(KeyCode.E)) {
Debug.Log("You got a weapon");
}
}
private void OnCollisionEnter2D(Collision2D collision) {
if (collision.gameObject.CompareTag("Chest")) {
chestHelperMessage.SetActive(true);
currentChest = collision.gameObject;
}
}
private void OnCollisionExit2D(Collision2D collision) {
if (collision.gameObject.CompareTag("Chest")) {
chestHelperMessage.SetActive(false);
currentChest = null;
}
}
private void Update() {
CheckKeyPress();
}
private void CheckKeyPress() {
if (chestHelperMessage.activeSelf) {
CheckInput();
}
}

script to turn the light off and on [ C# , Unity 3d ]

I made this little script that should turn the light of a point light off and on ... unfortunately only the off button works, after which it doesn't turn on again .... maybe there is some error in the script?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class TurnLight : MonoBehaviour {
public GameObject light;
private bool on = true ;
void OnTriggerStay(Collider other) {
if (other.tag == "Player" && Input.GetKey(KeyCode.E) && !on)
{
light.SetActive(true);
on = true;
}
else if (other.tag == "Player" && Input.GetKey(KeyCode.E) && on)
{
light.SetActive(false);
on = false;
}
}
}
Please Check this link to understand OnTriggerStay Function
On Trigger Stay
Also Please check those to differentiate between
Get Key and
Get Key Down
the problem is calling of OnTriggerStay happens continuously and you are using GetKey functions which will return true all times if the player pressed.
So when you use GetKey this will cause Single press on E will be redden multiple times.
Here is my solution
public GameObject light;
bool isPlayerStandingNear = false;
private void Update()
{
if(isPlayerStandingNear)
{
//Debug.Log("isPlayerStandingNear");
if(Input.GetKeyDown(KeyCode.E))
{
Debug.Log("Light State : "+ light.active);
light.SetActive(!light.active);
}
}
}
void OnTriggerStay(Collider other)
{
if (other.tag == "Player")
{
isPlayerStandingNear = true;
}
}
private void OnTriggerExit(Collider other)
{
if (other.tag == "Player")
{
isPlayerStandingNear = false;
}
}

Method OnTriggerStay2D() works only when player is moving

I made a simple "radiation" field through a trigger but it works only when player in move. Function countes two decimal values.
What could be the problem?
decimal radIntensity = 0.001m;
decimal currentDose;
private void OnTriggerStay2D(Collider2D area)
{
if (area.gameObject.tag == "Player")
{
currentDose += radIntensity;
}
print($"Current dose: {currentDose} R");
}
In the RigidBody2D component there's an option to disable or enable the sleeping mode.
Try to disable it with the RigidbodySleepMode2D.NeverSleep option.
Also can use a boolean-flag and OnTriggerEnter2D and OnTriggerExit2D. But never sleeping rigidbody is much better
bool _flag;
private void Update()
{
if (_flag)
{
currentDose += radIntensity;
}
print($"Current dose {currentDose} R");
}
private void OnTriggerEnter2D(Collider2D area)
{
if (area.gameObject.tag == "Player")
{
_flag = true;
}
}
private void OnTriggerExit2D(Collider2D other)
{
if (other.gameObject.tag == "Player")
{
_flag = !true;
}

How Do I Destroy The Collided GameObject When The Pickup Button Is Pressed (X) In Unity

I need to make my GameObject (pickup) get destroyed when the player enters its trigger and presses x to pick it up.
This is what I've got so far.
this is on the pickup that needs to be destroyed:
public void Update()
{
if (UIManager.XButtonPressed == true)
{
UIManager.AbleTopickUp = false;
UIManager.XButtonPressed = false;
Destroy(this.gameObject);
Debug.Log("Destroyed the pickup.");
}
}
void OnTriggerEnter(Collider collision)
{
if (collision.gameObject.name == "Player")
{
UIManager.AbleTopickUp = true;
}
}
void OnTriggerExit(Collider collision)
{
if (collision.gameObject.name == "Player")
{
UIManager.AbleTopickUp = true;
}
}
and this is on the Player That Is Colliding with the Pickup's Trigger:
if (UIManager.AbleTopickUp)
{
if (Input.GetButtonDown("XButton"))
{
UIManager.XButtonPressed = true;
if (currentSlot == 1)
{
Slot1_W = 1;
}
if (currentSlot == 2)
{
Slot2_W = 1;
}
if (currentSlot == 3)
{
Slot3_W = 1;
}
}
}
Any help will be appreciated
Replace OnTriggerEnter with OnTriggerStay

Activate script in collided gameObject

How can I activate part of the code in other script?
Attention: I don't need to activate all scripts. For example, a bunch of bullets flies around a player. And when one collide with a player, then need to activate part of the script of the collided object (that touched the player).
PlayerControl.cs
void OnCollisionEnter2D(Collision2D col)
{
if (col.gameObject.tag == "Bullet1")
{
lasthit = 1f;
// I need to activate Destroyy() in Bullet1 script
}
}
Bullet1.cs
public void Destroyy()
{
Debug.Log("Destroyed!"); // I need to activate this part of the code
} // ONLY in Bullet1.cs
The code below shows how to do it:
void OnCollisionEnter2D(Collision2D col)
{
if (col.gameObject.tag == "Bullet1")
{
lasthit = 1f;
// I need to activate Destroyy() in Bullet1 script
// HERE'S HOW:
if(col.gameObject.GetComponent<Bullet1>() != null)
col.gameObject.GetComponent<Bullet1>().Destroyy();
}
}
Just getComponent is be easier
void OnCollisionEnter2D(Collision2D col)
{
Bullet1 b = col.gameObject.GetComponent<Bullet1>();
if (b == null)
{
return;
}
lasthit = 1f;
b.Destroyy();
}
Actiovating bool in Bullet1 script
Player.cs
void OnCollisionEnter2D(Collision2D col)
{
if (col.gameObject.tag == "Bullet1")
{
col.gameObject.GetComponent<Bullet1pt().booldestroy = true;
}
}
Bullet1.cs
public bool booldestroy;
private void Update()
{
if (booldestroy)
{
Destroyy();
}
}

Categories

Resources