I have problem with triggers in my 2D game in Unity. I want to make enemy die when he triggers with player's weapon. The problem is there are two colliders attached to enemy (tagged "Enemy"):
one is box2d collider which is used as normal collider
second is sphere collider which is set as trigger and is used in script to check whether there is a player in range
I got sword object, which has sprite renderer, box collider (set as trigger) and script:
void OnTriggerEnter2D(Collider2D other)
{
if(other.tag == "Enemy")
{
if(!other.isTrigger)
{
Debug.Log ("enemy");
Destroy (other.gameObject);
}
}
}
Screenshot of scene:
http://i.stack.imgur.com/eVtRX.jpg
Screenshot of Enemy gameObject:
http://i.stack.imgur.com/9R5a6.jpg
So in general it sometimes works, but sometimes doesn't. When I disable sphere collider at enemy, everything works great, but I need to have it to check if there is player in range. How can I fix it?
you should Make sure two things in OnEnterCollider2D
1) Make sure both the gameobjects participating in OnEnterCollider2D must not be destroyed. If one has to be destroyed then it should be destroyed with some time later.
2) Make sure one of the game object participating in collisions has to have rigidbody attached with isKinematic unchecked.
Related
I'm making a game in unity where I added a spotlight that is constantly rotating at the x-axis and I want the light to detect my player when the light hits the player
i tried raycasting but its just a single line so there is no accuracy
You can have a cone mesh
-> use it as mesh in MeshCollider trigger without any mesh renderer
-> now you basically have an invisible collider object which is a trigger so other objects can still just pass it without actually colliding
-> however, now you can simply check for collider overlapping using e.g.
private void OnTriggerEnter(Collider other)
{
if(other.TryGetComponent<Player>(out var player)
{
Debug.Log("Hey {player}, I see you!");
}
}
This would be your pre-filter for checking if any further work is required at all - if there is no player at all within this trigger then there is nothing further to check anyway.
Then further checks could e.g. include also checking if there are multiple hit objects, if only the player is in there also nothing else to do as you know you already see the player. If a wall object is closer than the player only then you can do further raycasting checks if maybe you see any edge of the player etc
For example for staters making a raycast on each edge of the player and check if one of those hits the player and not the wall
I have a player object that I put on the "Player" layer. I unticked Player/Player in the collision matrix(not the physics2D one).
That made it so I could walk through another player without colliding.
But I noticed that if I jump ontop of another player then I get stuck on the "head". https://gyazo.com/112d91b4edc3bb5e1de827ff69b9297d
I tried debugging from an OnCollisionEnter function but it shows nothing while I am ontop of the other player and It printed when I was on the ground.
private void OnCollisionEnter(Collision collision)
{
print(collision.gameObject);
print(collision.gameObject.layer);
print(collision.gameObject.GetComponent<Collider>());
}
Disabling the collider on one of the players at runtime obviously made me fall through the other player but I can't have it like that.
The raycast used to detect the ground for the purposes of handling character physics does not have the correct layerMask.
It needs to include the player layerMask so that it does not result in a false positive for contact with the ground.
so I've got an enemy in my 2.5d platformer, that I already got to follow the player around, now I want him to jump over small obstacles, and honestly, I have no idea how to. My enemy is a sphere with rigidbody and a box collider scaled 2x in X axis, effectively making it 2 times wider than the sphere. I want this box collider to be a trigger and for it to launch a AddForce.
Where do I put the code for the trigger - if I put it in void update() wouldn't it apply force until the sphere is above the obstacle? I only want it to apply the force once.
Also, what would the code for the trigger look like?
So far I only got figured out how to find the rigidbody by getcomponent and apply force to it, and I watched a video on triggers, but it didn't really help :/
Use OnTriggerEnter method like this:
void OnTriggerEnter(Collider other)
{
if (other.tag == "IMakeEnemyJumpBeforeAnObstacle")
{
// add force here
}
}
Note: this code should be in your enemy script. Plus use IMakeEnemyJumpBeforeAnObstacle tag on the trigger object which will be placed right before the obstacle. And make sure both the enemy and the other object have property IsTrigger checked from inspector.
Learn More
Hope this helps
i was making a pong game but i am coming across a problem. When my ball (sphere collider) collides with one of the side walls (Box collider - trigger object), i want to know which wall it collided with (i put that tag in both walls). The problems is, OnTriggerEnter() method only tracks the object that is not the trigger. How should i get the information on which wall my ball collided with? I need to specificily know whether it was wall a that the ball collided with or was it walla b that the ball collided with. Any help would be greatly appreciated.
You have the gameobject inside the collider
void OnTriggerEnter(Collider other) {
Debug.Log(other.gameobject.name);
}
Using Unity 5.0.1f1
I'm trying to make it so that when I shoot, if it hits an invisible object it destroys it, but when it collides nothing happens. Here is the code:
void OnTriggerEnter2D(Collider2D col){
Destroy (col.gameObject);
Debug.Log ("find");
}
It's hard for me to give you an answer given the little information that you are providing. But, the most possible solutions to your problem are:
Attaching a Rigidbody component to one of the colliding objects.
Making the object which will be destroyed have a normal collider attached and the other object must have a trigger collider attached.
Make sure that all of the Colliders/Triggers and Rigidbodies you use are 2D. That is, Rigidbody2D and Collider2D. Because you are using the void OnTriggerEnter2D method.
Make sure that the colliding objects are in layers which collide with each other. You can check which layer collides with each layer by going to Edit-->Project Settings-->Physics2D (Or Physics if you end up using 3D Physics).
Hope this helps!