I have been working on this problem for some days now and would appreciate it if anyone can help me out. So, I am trying to make a simple board game on unity and trying to trigger an event (ex. getting a card) when a player stops at a specific waypoint. I have a boolean for hasStopped in another class called (Moving) that becomes false when the dice are clicked and true when the player has stopped moving.
Now, my game used to work with this code:
public void OnTriggerEnter2D(Collider2D collision)
{
if (hasStopped)
{
GetCardmethod();
}
}
but I'm not sure what I changed that this method stopped working. I checked in other methods to see that my boolean is getting updated properly, and I'm not sure what the problem is. I think my collider is detecting the boolean too early before it changes, but I don't know what to do to make this work. Like how would I get this game to only trigger the event if the player lands on the specific waypoint with the dice? Thank you
Check this solutions if they work for you:
If you have a 3d collider then you should use OnTriggerEnter() rather than OnTriggerEnter2D()
If your boolean hasStoped is in another cs script then you should make a reference to that script and access the boolean from there.
You might also want to check of the collision detection is continuous.
Related
So me and some people are developing a VR game and we need to have it when the VR hand Presses the button Some objects are hidden so when the trigger on the collider is entered
So I have the function
public void update()
{
//Called once per frame
}
Private void Ontriggerenter(collider, other)
{
if ( other.tag == "Hand")
{
Copper.SetActive(false);
}
Where copper is a public game object
I still have the start and update functions not sure if that's the problem but the button clicks but the function is never triggered dose someone maybe know the problem
the "T" and "E" of OnTriggerEnter should be capitalized and OnTriggerEnter takes a variableof type Collider. I am not sure if you have configured the colliders correctly. Here is how the code should look like
Private void OnTriggerEnter(collider other)
{
if ( other.tag == "Hand")
{
Copper.SetActive(false);
}
}
You can check out this article on Unity Collision basics to get some basic idea.
In order for a Trigger collision to happen, both objects need to have colliders, at least one needs to have a non-kinematic Rigidbody component on the same object as the collider, at least one of the colliders needs to have the "IsTrigger" property set to true (checked).
Here is a link to a Chart about how to get different types of collisions.
Note that there is a difference between a Trigger and a Collision (OnTriggerEnter vs OnCollisionEnter).
I removed the trigger and just using the positions of the VR button and called the setactive when the pos changed. Thank you all for your help tho.
I can't get a collision detection to work between and Enemy object and a Projectile object. I tried looking online for anyone else having similar problems, but haven't been able to find anything that relates to the problems I'm having.
I'm not getting any errors in the console or warnings.
In my c# code that's a component to and object with a Rigidbody2D and CircleCollider2D I have
void OnCollisionEnter2D(Collision2D collision)
{
print("collision");
if (collision.transform.tag == "Projectile") {
print("Collision with Projectile");
}
}
Not getting collision prints from the code above to print to console.
I have another object that has RigidBody2D and CircleCollider2D as well. It also has the tag "Projectile". When the collider areas overlap with each other nothing happens.
Enemy: RigidBody2D, CircleCollider2D, C# code above.
Enemy:Tag: "Projectile", RigidBody2D, CircleCollider2D: IsTrigger - true.
Have you checked your rigidbody properties?
I know some of the properties that you can change on that effect the way collisions work, try turning off simulated if that one is on for a start.
You have isTrigger checked in collider component. OnCollisionEnter doesn't work when trigger is enabled. There's another function OnTriggerEnter if you want to use trigger.
OnTriggerEnter
OnCollisionEnter
Don't confuse these two.
I've been tearing my hair out for weeks just trying to detect a collision between a RigidBody and a BoxCollider tied to a spot light that is tied to the camera, I want to detect when the player is flashing their flashlight at something, but for some reason this doesn't work.
I don't think it's detecting the collision at all, the variable "test" does not change and nothing appears in console, the flashlight hitbox I'm sure is large enough, but the console still gives no indication of anything happening, I was following this tutorial: https://www.youtube.com/watch?v=QRp4V1JTZnM
and here's the simple code I made:
void OnCollisionEnter(Collision col) {
if (col.gameObject.name == "Spot_Light") {
Debug.Log("detected");
test = 375;
}
}
If you marked trigger in your collider, you cannot use the OnCollisonEnter to detect a collison, you should use the OnTriggerEnter instead.
I have a 2d box collider on a "Magnet" sprite and a circle collider on a "Ball" sprite. On the Magnet sprite, I turned on "Is Trigger" and tried a few rigidbody settings: Kinematic, Static and Dynamic. The Ball also has a Dynamic rigidbody on it. I'm using Unity 2017.3.0f3.
For the time being I just want to check if an object has entered the Magnets collider and log the object's name.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Magnetism : MonoBehaviour {
void OnTriggerEnter2D(Collider2D other)
{
Debug.Log(other.name);
}
}
I also tried using the OnCollisionEnter(Collision other) and it still doesn't do anything. Oddly I have no errors as well. Oh, also in one forum, I found someone mention that using a onCollisionEnter/onTriggerEnter. That didn't work for me either.
Here is the checklist to make sure your collision works
Make sure the layers your gameObjects are in are set to collider under Project Settings -> Layer Collision Matrix.
Make sure at least one of the colliding bodies have a Rigidbody2D (not rigidbody, must be 2D) attached to it
Both of the objects must have a Collider2D
Make sure the Magnet's collider has IsTrigger set to true
The script in which the IsTriggerEnter2D function is present must be on the same object which has the collider2D (Also make sure this collider is set as a trigger)
Make sure the size of the colliders isn't 0 or is big enough. This can happen if you create a 2D sprite first and put the actual sprite image later
Make sure all of the components and gameObjects are enabled/active
I know some of them are trivial but still easy to miss
Edit: One more thing; the rigidbody2D must have simulated set to true.
I've got two objects (magnet with box2D and ball with circle). The magnet object has a script attached that does the same as you. Everything works as it should. (Tested with your version.)
Just try to double check everything and otherwise, try to create a new project and see if it occurs. If that doesn't work, test with 2017.3.1f1. If all that does not work, with I highly doubt, send a bug report to Unity and they'll help you.
Ensure that all colliders and rigidbodies you're using are 2D.
In order for the OnTriggerEnter2D to work, at least one of the colliders must have the Is Trigger active.
To make a quick test, you can disable all rigidbodies and check with only the colliders active.
The script code is perfectly fine and should work.
How can I place specific points on my 2D tile map that are invisible and do not obstruct any movement in the game, and when the player interacts with this it fires some code. Any help would be great, thanks!
Let me know if more description needed
You probably want to use a BoxCollider2D as a trigger (i.e. set the IsTrigger flag to true). You can override the OnTriggerEnter2d method to fire off your code when something enters the bounds of the collider.
Setting the IsTrigger flag will cause the collider to NOT block any actors. It will simply trigger the events as the collider is entered / touched / exited etc.
See the API here