Unity 3d collision detection - c#

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.

Related

unity c# OnCollisionEnter2D() Not working

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.

Child position is moving along with parent, but not actually moving (moving platform)

So I have been stuck on this for the last few days, researched and tried a lot of things but still can't figure this out.
I have a player with a Rigidbody and a moving platform with a Rigidbody as well. The platform is moving back and forth and I want the player to stay on it when he's on.
So when the player jumps on the platform, the platform become it's parent. This is working fine, no problem on that end.
private void OnCollisionEnter2D(Collision2D collision)
{
if (collision.gameObject.CompareTag("Moving Platform"))
{
transform.parent = collision.gameObject.transform;
}
private void OnCollisionExit2D(Collision2D collision)
{
if (collision.gameObject.CompareTag("Moving Platform"))
{
transform.parent = null;
}
}
The thing is, since the player has a Dynamic RigidBody, it looks like the movement of it's parent does not affect him. I can see his position moving in the inspector, but it's not really moving in the game.
When I set the body type to kinematic, the player stays on the platform but then I can't find a way to actually move while on it. So he gets basically stuck on the platform.
Is there an easy fix for this ? Or should I completely change the way I am making these moving platforms.
(BTW, every movements are handled with physics)
When you move the player sometimes it detach from collision even with small margin and will give weird results. try Physics2d.OverlapCircle instead.

Why is my bullet moving through my enemy and not detecting a collision?

I have my Unity 2D project set up so that the bullet is a trigger and will detect and collisions but not interact with the environment, and my enemy being a collider so that it will do both. I don't know if the fact that they are two different types affects collisions, but I thought I'd include that detail incase it did. I know this issue is not caused by my bullet moving too quickly since when I turn the speed of the bullet down the issue persists. I will include the code for the enemy collision and the bullet collision detection here:
void OnCollisionEnter2D (Collision2D coll)
{
if (coll.gameObject.tag == "Proj") {
Destroy(gameObject);
Debug.Log("Hit");
}
Now for the bullet:
void OnTriggerEnter2D(Collider2D hitInfo)
{
if (hitInfo.gameObject.tag == "Enemy") {
Destroy(gameObject);
}
The bullet is detecting a collision with an enemy and deleting itself, but the enemy is not detecting a collision with the bullet and taking damage/deleting itself. Even when I turn off the Destroy(gameObject) part of the bullet code so that it doesn't destroy itself on collision I still have the same problem so I know it isn't matter of it deleting before anything detects. I hope I was thorough enough with my explanation and that someone can help me resolve this issue.
Image of the inspectors: https://imgur.com/a/TLfcNcp
The bullet needs to be a non-trigger collider as you are calling OnCollisionEnter and not OnTriggerEnter from the enemy script.
Please show the settings of each collider, also OnCollisionEnter2D and OnTriggerEnter2D are 2 different things, and will occur in different conditions, so make sure that you are using the right one for the player.
I'm not entirely familiar with trigger behaviour, as it's been a while since I've played around with those, but this bit of documentation for MonoBehaviour.OnTriggerEnter2D(Collider2D) is an important starting point for solving your problem:
Trigger events are only sent if one of the Colliders also has a Rigidbody2D attached.
I can't say for sure, but your screenshots suggest that there is no Rigidbody2D involved.
Interestingly,
documentation for Collider2D.OnTriggerEnter2D(Collider2D) neglects to specify that a Rigidbody2D is necessary. I'm not sure what this implies; both MonoBehaviour.OnTriggerEnter(Collider) and Collider.OnTriggerEnter(Collider) (the 3D analogues) are said to require a RigidBody. Both of these pages also claim
If both GameObjects have Collider.isTrigger enabled, no collision happens.
However, the example code from Monobehaviour.OnTriggerEnter2D(Collider2D) claims that setting both 2D colliders as triggers will still allow the function to be called.

Unity tranform.forward is constanly changing

So I'm working on a door opening system where I check trough a Raycast if the user has the middle of the screen focust on the doorknop. Here is the bit of my code that isn't working:
private void Update()
{
if (Physics.Raycast(Camera.transform.position, Camera.transform.forward, out rayHit, Distance))
{
Debug.DrawRay(Camera.transform.position, Camera.transform.forward, Color.blue);
Collider col = rayHit.collider;
if (col.tag == DoorKnopTag)
{
DoorIndicator.SetActive(true);
}
else
{
DoorIndicator.SetActive(false);
}
}
else
DoorIndicator.SetActive(false);
}
So I didn't put the code in that checks if the mouse is clicked when the player selects the doorknob but I'm sure that isn't the problem.
The problem is that when I check the Ray in my scene the line is flickering very hard. I debugged a bit and came the conclusion that the Camera.transform.forward is constantly changing even if I don't change the Camera.tranform.position. When I changed the direction of the Physics.Raycast to a constant Vector3 it didn't flicker anymore so I think that the problem is in the transform.forward. But I can't see how to fix it.
Try using FixedUpdate() instead of Update().
For physics events you should use FixedUpdate(),
For graphical events you should use Update().
The reason of raycast flickering is caused by casting raycast from a collider most of the time. Check whether you have a collider on raycast origin point or not.

Unity parenting and deparenting game objects

I'm currently implementing a wall climbing system into my 2D game and I'm stuck on a few of the trickier parts.
My thinking is that I would have my player parent to the wall object when it collides with the 2D collider attached. When the player collides with the wall, the player becomes a child of that wall and is limited to only moving up and down on the wall. When the player jumps, or reaches the top, they are no longer a child of the wall. But the player has the ability to jump on any point onto the wall they land on and stays at that point.
Right now I have the parenting part worked out with the following code (this code is attached to the player):
void OnCollisionEnter2D(Collision2D collision)
{
if(collision.gameObject.tag == "Wall")
{
this.transform.parent = collision.transform;
Debug.Log("hit a wall");
}
}
The two areas I'm struggling with are de parenting my player from the wall and having the player still to the position on the wall where they land.
With the first part (de-parenting) I believe I'll need to make use of the following code:
void OnCollisionExit2D(Collision2D collision)
{
if(collision.gameObject.tag == null)
{
this.transform.parent = null;
Debug.Log("not hitting anythin");
}
}
However, when I run this, my player doesn't deparent right away. Am I doing it correctly?
I'm also clueless as to how to begin my other problem of having the player stick to the part of the wall they connect with. Can someone please help me with my issues?
Ideally you'd use tranform.SetParent for the extra you get over position.
transform.SetParent(parentTransform, true);
transform.SetParent(null, true);
if the latter line is fired it should detach from it's parent to the best of my understanding and the 2nd parameter on SetParent should fix positioning issues.
Though your existing code should at least change the hierarchy, I suspect the issue there is your collision code - if you enter a collision with Tag:Wall then you're going to exit a collision with Tag:Wall not Tag:Null.
However #limoragni is right, editing your hierarchy is going to have no effect on the possible movement of your character unless you've specifically coded that in yourself. You could just as easily set a boolean and use your code with that.
Ref:
http://docs.unity3d.com/ScriptReference/Transform.SetParent.html

Categories