In my game my player avoid the obstacles and move towards the goal, but when i play the game in device its started colliding the game.
I tried to collide in editor but its not the issue anymore. here is the picture for detail info alt text
this is my player passing aside of an obstacle but in device its triggered the collision but its not colliding with the object.
What could be the issue, i really have no idea what's going on..
I am using OnCollision2D to check the collision.
Edit: Here is Rigidbody2D inspector
Related
I am making a simple Unity 2D game. In this there are three gameOjects - 1) The Ground (a long rectangle)2) The player (a square)3) The coin (a circle)Currently i am using colliders on all three. These colliders help the player remain on the Ground and when the player collides with the coin i increment the score.But the problem is that when the player collides with the coin it behaves as if the coin is a physical object and it stops the player.I dont want thisI want that when the player collides with the coin there is no physical interaction between themI tried one thing - switching the colliders in player and coin to isTrigger, but in this case the player and the ground interact in the way they should (the player falls through the ground).What should i do??
Make the interactable objects triggers. That way, you can interract with them, without obstracting your path. To do that, go to the collider Component on the inspector and check the "Is Trigger" checkmark.
Then, go to your scripts and instead of the event OnColliderEnter use the event OnTriggerEnter
I have a bullet game object and an enemy game object.
I want to have the ability of getting the collision point/contact, which's why I think I need the collider of the bullet to not be a trigger collider - this way, I want to have the bullet bounce off the enemy. At times, the bullet will need to bounce off of rectangular objects (not a problem), but I also want the bullet to bounce off of elliptic objects (the problem).
I want the bullet not to "physically" push the enemy, which is why, intuitively, I should set the bullet's collider to be a trigger collider.
When the bullet's collider is a trigger collider, I seemingly don't have a way to implement the bouncing.
But when the collider's not a trigger collider, it pushes the enemy object.
There's no code errors, but I think the code hasn't got anything to do with the problem.
I suspect the problem may have something to do with the Physics Collision Matrix.
EDIT
It seems that raycasting would be the solution to the problem, as Leoverload proposed. My problem became a different problem now, so I'll close off this thread, and open a new one.
Thanks for the help, #Leoverload ! :D
For the position of the hit it's very easy. You must have 2 colliders and 2 Rigidbody and then you can simply add a
Void OnTriggerEnter2D(Collision Coll) and inside it check for the tag : if(coll.compareTag("surface")) and inside you can get the position with collision.transform.position.
The collision matrix on the Physics menu only allows to remove Collision between certain layers, it's useful if you want the enemies not to push eachother!
If you don't want the player pushed you can change the collider to trigger, and destroy the bullet just after the collision with the same method as before with a void OnTriggerEnter2D and the compareTag. In this way it will look like it touches the enemy and explode, but it won't actually touch anything. If you want to add knockback to the player then you could do a simple AddForce based on the direction of the bullet and the work is done!
If you want to avoid the bullet to move walls you can set walls to static and it should work fine
Im having trouble with colliders and rigid bodies in my game.
I have a powerup that when a player touches gives the player a weapon upgrade, this is simply achieved with onTriggerEnter2D() and setting the collider on the powerup as a trigger. But the problem with that is the trigger does not even detect the ground collider on which the player is running and it just falls through the ground.
When I disable the trigger on the collider, the powerup, when spawned, falls to the ground and stays on it as normal, but then the player can't pick it up as it is just pushed around rather than being run through.
I have a similar problem with my enemies that should kill the player on touch. When I set the collider on them as triggers then they kill the player as expected, but fall through the ground since they have trigger colliders. But when I remove the trigger option, the enemies run on the ground as normal but never touch the player as the colliders of the enemy and that of the player push each other away and the gameObjects never actually touch.
How to solve this?
Thanks
You can use the method OnCollisionEnter2D(Collision) to detect collisions with non-trigger colliders, instead of OnTriggerEnter2D(), and it will still allow you to have your objects collide with the floor and not fall through.
Currently I'm making a racing game.
I move my cars with the Transform.translate class.
Now the thing I want for my cars is not to move through each other.
I attach colliders and a RigidBody to my player car and it's working.
But my problem is that each time my CPU cars and player car encounter with each other, my player car shows unrealistic behavior like moving out of the screen or throwing away.
I know this is part of the physics engine behavior but is there a way to make the RigidBody only do one job and make objects not to move through each other not to add other physics behavior?
Any ideas?
There are just few problems:
1.You don't move Rigidbody with transform.translate. When you do this, colliders will go through other colliders.
You move Rigidbody with Rigidbody.AddForce, Rigidbody.velocity and Rigidbody.MovePosition. By moving Rigidbody with these methods, collision should be functioning properly.
2.You are not even supposed to move the Rigidbody of the car.
Use WheelCollider. There are many online tutorials on how to set them up on the internet but Unity's documentation is fine too.
WheelCollider.motorTorque is used to move the car forward or backwards.
WheelCollider.steerAngle is used to steer the car.
WheelCollider.brakeTorque is used to brake the car.
If you need further help, you can find a fully working WheelCollider samples here.
I am building a simple game where I want to AI enemy to chase and run to my main character and attack it but before I can get to the point where it is attacking I need to figure out this problem.
When my enemy gets to my main character it keeps going forward and keeps pushing my main character back instead of stopping how can I fix this? Both characters have rigidbodies and colliders.
Thanks
The enemy will never be at 0 distance to the player, because the colliders of both player and enemy. It's "impossible" and this means that the enemy will keep on running forward into the player forever.
To fix that, you should set a minimum distance for the enemy AI. This way, the enemy won't move unless the player is out of range.
If you're using Unity's default AI or any other kind of AI from the Asset Store, you will probably find this setting on the script controller. Just look at the inspector and check if you can modify it.
You could try to use "OnCollisionEnter". As far as I know, you could use collider as a trigger of a future action of the enemy and player. Maybe you could set collider in a wider range than that of the characters of yours. Set a specific range that when the player is within that range of the collider it would chase the player and would attack it once it has come to a specific distance then perform the script that you want.