Unwanted collisions from colliders - disabled in matrix and layers are checked - unity3d

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.

Related

how to detect a player in unity when a spotlight hits the player

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

Unity composite collider 2d - Collission detection with onTriggerEnter or Raycast

I have found other people asking for this on the internet but I haven´t found a solution. I have a 2d game using tilemaps and composite collider. But when I add a composite collider I can´t use ontriggerenter or raycasts to detect the ground. So I have completely removed the ability to jump because there is no way of knowing if the player is on the ground or not, resulting in the ability to jump again before landing. Has anyone found a way around this?
There shouldn't be any issues with your approach. Without code examples all I can give you is some general pointers:
OnTriggerEnter
Use OnTriggerEnter2D(Collider2D col) to detect Trigger enters in a 2D environment.
OnTriggerExit2D for leaving, OnTriggerStay2D for every frame you're still colliding with other colliders.
Don't forget that one of the objects need to have a Rigidbody2D (if you don't want to use physics, select "Kinematic" in the dropdown and check the "Use Full Kinematic Contacts". For improved collision detection, change the Collision dropdown to "Continuous".
Raycast
When using Raycasts you need to, either start the raycast from a position that doesn't include your own hitboxes, or make sure the raycast ignore its own layer.
// Raycast down ignoring Player layer
int layerMask =~ LayerMask.GetMask("Player");
RaycastHit2D hit = Physics2D.Raycast(transform.position,
Vector2.down,
layerMask: layerMask);

Raytracing not responding correctly

So I'm new to Unity and I'm sure I'm missing an easy step, but after looking online for a while for some reason I can't find the solution.
I have two objects on the screen, player and enemy. Both have Rigidbody2D, and Box Collider 2D attached. On Box Collider 2D I have clicked is triggered On Rigidbody2D for both I have clicked Is Kinematic. On player I have a simple movement script. On the enemy object I have this:
void Update () {
RaycastHit2D hit = Physics2D.Raycast(transform.localPosition,transform.right,Mathf.Infinity);
Debug.DrawRay(transform.localPosition,transform.right);
if (hit)
Debug.Log(hit.collider);
}
Now for some reason when I move player over the object if (hit)
is true, but if I move the player anywhere on the right side it is not true. What would be the reason for this? Thanks.
First of all you don't need Rigidbody for raycast detection, only colliders. Second, Physics2D.Raycast uses world position, not local, so replace "transform.localPosition" with "transform.position", this messes it up a lot if the transform is child of something. Take in mind that you are sending raycast from the right side of your transform, so maybe it's not hitting anything and values you are getting are actually correct.

Unity OnTriggerEnter2D sometimes doesn't work

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.

Collision of object with player

So I have a bit of an issue with some unity scrip. I have a moving object, a ceiling to be specific, and I have it lower and 'crush' the player. However, it's not working. The ceiling moves, but nothing happens when it collides with the player. It used to work if the player was moving when it hit them but it isn't any more and I haven't changed any of the script so I don't know why that's happening.
Interesting suggestion from your teacher. I would put a character controller on your player, which you seem to have already. Then I would add the collision code:
void OnCollisionEnter(Collision c)
{
// die.
}
Then add a box Collider and Rigibody to the roof. I would also add this code above to the roof.