I do not know How to call the OncollisionEnter 2D Function in other places of the code.
In THIS CODE I WOULD LIKE TO CALL IT IN THE VOID UPDATE.
I am doing a PLATFORM Jumping game so ot is crucial for me to know how to call the oncollisionenter2D function.
void OnCollisionEnter2D (Collision2D col)
{
if (col.gameObject.name == "Front_Buildings")
{
GetComponent<Animator>().SetBool("isGrounded", true);
}
}
You shouldn't really be explicitly calling OnCollisionEnter2D.
If you need similar behavior, I suggest refactoring to something like this.
void Update() {
if (someReason) {
HandleCollision(someGameObject)
}
}
void HandleCollision(GameObject gameObject) {
if (gameObject.name == "Front_Buildings")
{
GetComponent<Animator>().SetBool("isGrounded", true);
}
}
void OnCollisionEnter2D (Collision2D col)
{
HandleCollision(col.gameObject);
}
Related
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();
}
}
I want to display a text in unity by a trigger, but it isnt working, this is my code. I have a collider with "Is Trigger" activated, the tags are ok, i dont know what its happening...
public class LaptopTriggerCollider : MonoBehaviour
{
public GameObject UiObject;
public GameObject cube;
void Start()
{
UiObject.SetActive(false);
}
void OnTriggerEnter(Collider other)
{
if (other.tag.Equals("player"))
{
UiObject.SetActive(true);
}
}
void Update()
{
}
void OnTriggerExit(Collider other)
{
UiObject.SetActive(false);
}
}
Firstly try to debug by putting a log statement in OnTriggerEnter function
void OnTriggerEnter(Collider other)
{
Debug.Log("Is this even being triggered?");
if (other.tag.Equals("player"))
{
UiObject.SetActive(true);
}
}
I presume it won't print in console, reason could be that you are missing a rigidbody in the collision.
A rigidbody is a must for collision or trigger events to be generated in Unity.
Instead tag.Equals use CompareTag
if(other.CompareTag("player"))
{
UiObject.SetActive(true);
}
CompareTag
I have got some triggers and I want them dependant on tags. This is what I have so far:
void OnTriggerEnter(Collider other)
{
Destroy(gameObject);
}
I want to check to see if the gameobject triggering it has a certain tag
The API is your friend!
Use CompareTag
the example is more or less exactly your code
void OnTriggerEnter(Collider other)
{
if (other.gameObject.CompareTag("Player"))
{
Destroy(other.gameObject);
}
}
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 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();
}
}