I'm trying to make a generic "Snake" game using Unity in order to reinforce my knowledge of C# and Unity fundamentals.
The problem I'm having is that I can't seem to get the player to Game Over when colliding with the body. Currently I have the collision check set up like this:
void OnTriggerEnter2D(Collider2D other)
{
if (other.gameObject.CompareTag("Border") || other.gameObject.CompareTag("Body")) //if the player collides with a Border tag or Body tag...
{
//Game Over Sequence
GameOver();
}
else if (other.gameObject.CompareTag("Collectable")) //otherwise if the player collides with a Collectable tag...
{
//Snek needs to grow!
shouldGrow = true;
}
}
Here's a picture of the snake body's prefab inspector as well. I checked to make sure the tag is set to Body.
The border collision is working fine, and the strange part is setting the border object to the "Body" tag is also working fine, so I'm not sure why the body segments aren't triggering the Game Over sequence.
I'm new to programming, so apologies if this question is obvious, but I don't understand why this isn't working.
Thanks in advance!
EDIT: Per Programmer's request, here's an image of one of the Border's inspectors:
Tick 'is Trigger' on the Snake_Body's box collider. Without this the OnTriggerEnter2D function is not called. You have done this correctly on the border box collider.
Related
I am new to Unity and having problems with this method. Any help would be appreciated.
Currently, I have one box gameObject (customer) that falls down onto another box gameObject (table). The script with theOnCollisionEnter2D method is attached to the tableBox object and checks using tags if the customerBox has collided with it. The problem is that despite colliding, OnCollisionEnter2D does not seem to be getting called at all, as nothing is appearing from Debug.Log. Both boxes have Rigidbody2D and a boxCollider2D.
I have tried changing the Rigidbody2D property to dynamic and also double checking the tags are correct. It is also possible that the script for the tableBox is not running at all, which if is the case I am not sure how to fix and would appreciate suggestions.
Here is the method:
void OnCollisionEnter2D(Collision2D collision){
Debug.Log("in the method.");
if(collision.gameObject.tag == "customer")
{
Debug.Log("Collision detected!");
}
}
Your code seems to be good. Check the following
Both the Rigidbodys are set to dynamic.
Both the bodies have 2D colliders.
They are not colliding already.
Both Colliders are not set as trigger.
Check the layer collision matrix.(Edit>Project settings>Physics2D)
is the "IsTrigger" checkbox unchecked on your collider component ?
Check if the gameobjects are correctly configured (layers, colliders, tags, your script...) and if you got layers, check the physics settings ;)
I have my main character and added a capsule collider 2d so he would take damage when attacked. When the character is attacking, for example his body is out of the collider space and won’t take damage when hit. Please may you help me solve this issue, I’m very new to unity
One way of doing it is having two different hitboxes that does the same thing but with different shapes so they match two different states of your character.
Enable and disable them in a script when calling the attack using:
Public Collider2D attackCollider; //Drag your attack collider here in the inspector
Public Collider2D idleCollider; //Drag your idle collider here in the inspector
void AttackCollider() //call this fucntion to change to attack collider
{
attackCollider.enabled == true;
idleCollider.enabled == false;
}
void IdleCollider() //call this function to go back to normal collider.
{
attackCollider.enabled == false;
idleCollider.enabled == true;
}
If you want to I'm sure you can incorporate these two functions into an already existing attack function if you have one.
In 3D, you could simply make the Collider a child of any of your bones, it would get animated altogether.
But you mentioned Collider2D, so:
If you have a 2D Rig with bones, you can do the same as obove
If you have a Keyframe Animation, you could animate the collider to match your animation
i am trying to develop a melee combat game i am using edge collider and below code
i noticed that when i move or enable and disable the collider from the inspector everything goes but when i stand in my palace and use AttackCol.enabled = !AttackCol.enabled;
i cannot hit and the trigger function do not called
the only difference i see is the collider color when i add it from the inspector or while i am moving its color is normal but when i enable and disable it by code its color goes pale and do not do anything
public virtual void OnTriggerEnter2D(Collider2D collision)
{
if (DamageSources.Contains( collision.tag ))
{
StartCoroutine(TakeDamage());
}
}
Make sure that your Trigger Game Object is not marked as Static.
Remove virtual from function definition.
Create another Game Object for Trigger Component and try to change activation of whole the Game Object, not collider component
Stuff like AttackCol.enabled = !AttackCol.enabled; is clever, but it can go wrong when that one is called (accidentally) more than once. I suggest trying it out in the simplest form AttackCol.enabled = true; to make sure the error is not there. Later you can still make it more elegant again! :)
i added AttackCol.isTrigger = AttackCol.enabled;
after AttackCol.enabled = !AttackCol.enabled;
and everything geos ok right now
but i think it is a work around
but i need to know why the color goes pale ?
So I'm making a simple "puzzle" using lights. Simply, there are 3 buttons with lights on them (red, blue, green). Each button has it's own trigger volume but when I go to play, nothing prints that I even enter, stay, or leave the trigger. I've never used Collider Variables before so I feel like I'm doing something wrong (obviously or it would be working!). But then I just did "Collider entity" in the OnTriggerStay/Enter/Exit Method and it still didn't print to the console that my player was entering. Why are my Triggers not working?
Click here for the code I'm trying
Click here to see how I have it in the Unity Scene
Triggers only respond to other colliders that have rigid bodies on them.
Try adding a Ridgidbody component to your player and set it to kinematic.
OnTriggerEnter/Stay/Exit works when the Object has a Collider Component and BluePuzzle2 doesn't have that.
Also OnTrigger function gets a Collider as parameter. Check the reference page
So in order to make that work put a script on every light and on that script copy this function
void OnTriggerEnter(Collider col) {
if (col.CompareTag("Player")) {
print("Entered the trigger");
}
}
Hope it helps.
I am trying to make a scoring system for a game that I am making; it's a remake of Flappy Bird.
The issue I am having is, I have set up a sprite with a rigidbody2d and a box collider as the score; I am trying to make it so that when the character passes through that sprite; it will add one to the score however when the character attempts to go through it, it just get's pushed back to the other side of the screen.
This is because of the box collider of course but I am not sure of any other way of doing this without this method. Here is the code which detects if the character hits the pipe or the score line:
void OnCollisionEnter2D (Collision2D hit) {
if(hit.transform.gameObject.name == "Pipe(Clone)") {
die();
Debug.Log("hit");
}
if (hit.transform.gameObject.name == "Score(Clone)") {
Debug.Log("Score");
}
}
Everything logs perfectly fine however the problem is that the bird just faceplants into the score line which isn't what is supposed to happen, please try to help or advise what I should do with this.
P.S. I am a novice programmer; I sort of know what I am doing however not really good at debugging things and fixing them, Thank you.
You should check the "Is Trigger" on your Bird object collider in editor. And use:
void OnTriggerEnter2D(Collider2D col) {
if (col.transform.name == "Score")
Score++;
}
In the bird object script.