Unity2D: I dont want player to die when this exception occurs - c#

My player currently dies when it hits an enemy tagged with "Enemy" and restarts the level using the following script
if (other.gameObject.CompareTag("Enemy"))
{
Destroy(gameObject);
SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);
}
I wanted my player to kill the enemy when the bottom of my player hits the enemy, so i created a child gameobject called "Feet" and labelled it "Feet" as well, I also added an edge collider to it. I added the following script to it.
private void OnTriggerEnter2D(Collider2D other)
{
if (other.gameObject.CompareTag("Enemy"))
{
Destroy(other.gameObject);
}
}
Now the "Enemy" dies when the "Feet" collides with it, but my player also dies, how do i make an exception to the first script so my player doesnt die when "Feet" collides with the object first instead of the "Player"

You should restrict the collider of the player. It seems like your "Feet" is included in the "Body" of the player. So "Feet" and "Body" touch the Enemy at the same time.
So make the collider of the player smaller at the bottom.

Related

The weapon is not colliding with the AI

private void OnTriggerEnter2D(Collider2D collider)
{
Debug.Log(collider.gameObject.tag);
}
When it collides with the weapon (or anything) it should print the tag. The problem is that it doesn't print anything.
Here you can see the player (normal colours) hitting the AI (green) and I am 100% that the AI is within the bounds of the weapons hitbox.
The AI layer and the player layers do interact with each other.
This is the box collider on the weapon (ignore it being disabled)
This is the box collider on the AI.
I have tried using ontriggerenter2D, ontriggerstay2D, ontriggerexit2D, ontriggerenter, ontriggerstay, ontriggerexit, oncollisionexit, oncollisionstay. I honestly have no clue why it is like this.
I know that it is not entering the on collision enter because it should be printing the ground tag that I have on the ground. Also the game is in 2D.
I believe the rigidbody or the collider have to be on the same game object as the script for the collision/trigger enter and exit methods to be called.
Please check the following:
The OnTriggerEnter2D(Collider2D collider) is on a script that
inherits from MonoBehaviour
This same MonoBehaviour script is in one of the GameObjects (Player
or AI)
This script is attached to a GameObject that also has a Collider2D
component
Last but not least...
Trigger events are only sent if one of the Colliders also has a Rigidbody2D attached to it's GameObject as you can read it on the MonoBehaviour.OnTriggerEnter2D API

Walking on projectiles bullets, not colliding but player jumps on them due to step offset

Title says it all, the projectiles only hit the player and disappear if the player is standing still, if he is moving, the player will ''jump'' on top of the bullets and walk over them, not triggering a collision and not doing anything really.
What Im trying to achieve is that the bullets will passthrough the player, but still detect collision if they do collide with the player, so that the character controller can never walk on them, or they should just collide with the player before the player gets the chance to walk over them. This problem has to do with the "Step Offset" function in the character controller component, because when I set it to 0.05 this issue does not happen, but I cant do that because my character needs to be able to walk up stairs or over small ledges.
Thanks a lot!
void OnCollisionEnter(Collision other)
{
if (other.gameObject.CompareTag("Player"))
{
scrCh = other.gameObject.GetComponent<scr_CharacterController>();
calculatedDamage = Random.Range(minDamage, maxDamage);
if (scrCh != null && scrCh.currentHealth > 0)
{
CancelInvoke("Remove");
gameObject.SetActive(false);
scrCh.TakeDamage(calculatedDamage);
if (gameObject.CompareTag("Special Bullet"))
{
scrCh.TakeDamage(calculatedDamage * 3);
gameObject.SetActive(false);
}
}
}
if (other.gameObject.CompareTag("Unbreakable"))
{
gameObject.SetActive(false);
}
}
There are 2 ways to fix this issue:
Either change the size of the Character Controller Collision Box(Its a Cylinder), so that its size is smaller than your Character Mesh Collider, this way the bullets will collide with the Mesh instead of with the Character Controller.
Or you can set the Character Controller on a different Layer, one that does not collide with the bullets, and the result is the same the bullets will now only collide with the Character Mesh Collider.

Game object in triggered Box Collider 2D in unity

I have problem with 2d game in unity. There is sprite named Player with box collider 2D, rigidbody, moving... There is another sprite with two box colliders 2D. First isn't trigger, second is trigger. This sprite has this code:
private void OnTriggerEnter2D(Collider2D other)
{
if(other.CompareTag("Player"))
{
Debug.Log("Player in range");
}
}
private void OnTriggerExit2D(Collider2D other)
{
if(other.CompareTag("Player"))
{
Debug.Log("Player left range");
}
}
This is player
This is game object
I need to make that player in range of that object. This is only for debug but it's not working. I don't have any idea how to fix it. I tried docs but I don't know, I'm beginner. Thanks for help.
I see you are new to Unity. You have confused names of things. Let it put it straight for you.
Your Game Object is named Player
The Game Object's tag is Untagged
The Game Object has a Component called Sprite Renderer
The Sprite Renderer has a Sprite (the image) called playerDown_0
Your problem is that your game object Player does not have a tag.

Bullet not always hitting Enemy

void OnCollisionEnter(Collision col){
if (col.gameObject.name == "Enemy1") {
enemyDamage++;
GameObject clone = (GameObject) Instantiate (tempBloodSplat,enemyObj.position,enemyObj.rotation);
Destroy (clone , 0.5f);
if (enemyDamage > 3) {
anim.SetFloat ("Die", 0.5f);
Destroy (enemyObj.gameObject , 5.0f);
}
Debug.Log ("Bullet is hitting Enemy");
}
}
This is my code, i used on BulletObject
My Bullet Object has Collider
My Enemy has a Collider
My Enemy has a Rigidbody
Bullet is not having Rigidbody
I have problem that when i shoot, the bullet is hitting the Enemy in his range, like the circle under the body of the Enemy,Image
Sometimes the bullet hits correctly (means enemy is damaged), but sometimes the bullet moves out without making any damage to the enemy,I don't know why its happening.Does the velocity of the bullet has any effect on it..
Please help or guide me to solve this problem, Thanks
If you are not using a rigidbody on the bullet, then you are probably updating the bullets position vector directly, and what could be happening is the following:
. Since the bullet is not a rigidbody, Unity does not 'assume' it should behave like one and thus does not do an actual physics simulation of the bullet movement (which would probably include a raycast from start position to end position and colision checking in between). If you have a problem with adding a rigidbody to the bullet, then do the raycast yourself. You will even learn a bit of how the physics simulation behind unity actually might work!
Good Luck!

collision not triggered when i touch object

I have 1 First Person Controller as a player and created 1 cube as an enemy.
Enemy has rigidbody to assign gravity to it and it moving on 1 platform which is made from cube.
collision detect script is attached with enemy. when enemy touches the player method triggered successfully. but when Player touch enemy method is not called.
void OnCollisionEnter(Collision collision) {
Collider other=collision.collider;
Debug.Log(string.Format("OnCollisionEnter tag={0}",other.tag));
//
if(other.gameObject.tag=="Player")
{
Debug.Log("Player1 Touched");
}
}
I found the answer.
as I have used firstpersoncontroller i have to implement below method to find collision
void OnControllerColliderHit(ControllerColliderHit collision)
{
Debug.Log(string.Format("object tag={0}",collision.collider.gameObject.tag));
}
Thank you all for your help.
unity link that helped me.
http://docs.unity3d.com/Documentation/ScriptReference/MonoBehaviour.OnControllerColliderHit.html

Categories