I have a user navigating and colliding with a rigid body. I would like one object to slide along the other when the collision happens. It behaves this way when the isKinematic setting is NOT checked. However, the object then moves. I would like the object to stay in place when collided with. When I turn isKinematic off, the user is able to navigate through the rigid body which shouldn't happen. Any ideas on how to fix this?
As of now, I don't have any scripts associated with the rigid body.
If one object is moving and the other should be static:
RigidBody + Collider for the moving object
RigidBody (isKinematic) + Collider for the static object
So basically you are missing the Colliders to avoid GameObjects go through each other
Related
I have 2 simple capsules. 1 is stationary and the other is moving to the same tile on the tilemap using transform.position.
Both capsules have capsule colliders and rigid bodies. I've attempted to remove the rigid body but from what I can tell, the OnCollisionEnter function requires a rigid body to work.
My script, attached to both of these, is a simple:
private void OnCollisionEnter(Collision other)
{
print("Collision Detected! " + other.gameObject.name);
}
I've used combinations of 'isKinematic', 'isTrigger', placing one component before the other then reversing, ensuring I have the script attached to both objects, making sure my capsule collider is sized to the object, and looked at other people's issues to try their steps to fix my issue.
Nothing seems to trigger the collision between the 2 capsules. It's got to be something small I'm missing.
I have a tag on both objects and have tried things like if(other.gameObject.tag == "myTag") {...} but that's not working for me, either.
Can anyone spot my mistake? Let me know if you need to see anything else - happy to provide images or whatever will help. Thanks in advance!
Above is the basic identical hierarchy of both capsules with components attached to the capsule mesh. Both have the parent empty objects they're stationed under. (Which themselves, contain scripts but no collider or rigid body)
Here is an infographic to show when a collision message will be detected by OnCollisionEnter between two objects. Both objects will need some sort of collider, and will most likely need a Rigidbody.
You will not want to set isTrigger as that will not make it physically react to a collision, but will just detect when a collision occurs. It will also not call OnCollisionEnter but will call OnTriggerEnter. Setting both not as triggers, adding a collider, and giving them Rigidbodies should allow the collision to be detected. You will also need to attach this script to one of the objects that have the collider. Are there other components on the objects you are using?
Make sure you are not looking for
void OnTriggerEnter(Collision collision){}
I am making a First Person Shooter and I have a rigged player with a mouse look script, and I used to rotate the spine with spine.transform.rotation, but this method does not look for collisions, this means if I rotate and I am standing near to a GameObject, I glitch/bug through the object. So I tried to do it with a Rigidbody but this doesn't seem to work, I tried: rbSpine.AddTorque(new Vector3(mouseLook.y,0,0), ForceMode.Force);. This doesn't do anything. And I have another problem with the Rigidbody, I've only a rigidbody attached to the spine, my player has multiple simple collision and these overlap eachother a little, and my rigidbody is pushed away into the air slowly. Does anyone know a solution for this?
I put a rigidbody component on the player, this will take care of the collision detection for itself and all its child objects.
I have a 2d box collider on a "Magnet" sprite and a circle collider on a "Ball" sprite. On the Magnet sprite, I turned on "Is Trigger" and tried a few rigidbody settings: Kinematic, Static and Dynamic. The Ball also has a Dynamic rigidbody on it. I'm using Unity 2017.3.0f3.
For the time being I just want to check if an object has entered the Magnets collider and log the object's name.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Magnetism : MonoBehaviour {
void OnTriggerEnter2D(Collider2D other)
{
Debug.Log(other.name);
}
}
I also tried using the OnCollisionEnter(Collision other) and it still doesn't do anything. Oddly I have no errors as well. Oh, also in one forum, I found someone mention that using a onCollisionEnter/onTriggerEnter. That didn't work for me either.
Here is the checklist to make sure your collision works
Make sure the layers your gameObjects are in are set to collider under Project Settings -> Layer Collision Matrix.
Make sure at least one of the colliding bodies have a Rigidbody2D (not rigidbody, must be 2D) attached to it
Both of the objects must have a Collider2D
Make sure the Magnet's collider has IsTrigger set to true
The script in which the IsTriggerEnter2D function is present must be on the same object which has the collider2D (Also make sure this collider is set as a trigger)
Make sure the size of the colliders isn't 0 or is big enough. This can happen if you create a 2D sprite first and put the actual sprite image later
Make sure all of the components and gameObjects are enabled/active
I know some of them are trivial but still easy to miss
Edit: One more thing; the rigidbody2D must have simulated set to true.
I've got two objects (magnet with box2D and ball with circle). The magnet object has a script attached that does the same as you. Everything works as it should. (Tested with your version.)
Just try to double check everything and otherwise, try to create a new project and see if it occurs. If that doesn't work, test with 2017.3.1f1. If all that does not work, with I highly doubt, send a bug report to Unity and they'll help you.
Ensure that all colliders and rigidbodies you're using are 2D.
In order for the OnTriggerEnter2D to work, at least one of the colliders must have the Is Trigger active.
To make a quick test, you can disable all rigidbodies and check with only the colliders active.
The script code is perfectly fine and should work.
After updating from unity 5.5.2 to 2017.3 the Player character does not collide with objects(evironment asset) and is passing through them, all the objects have mesh collider with convex on, This is only happening in one of the level(scene) in the game while in rest of the level(scene) the player behaves normal and is colliding with objects.
Player settings
object settings
The player's rigidbody is kinematic. I would try by disabling that property.
I'm stuck on this sphere has a CircleCollider2D and rectangle has a BoxCollider2D i increase the offset of BoxCollider. But why sphere not collide from it's border. Sphere go inside and then collide just like this.
I want that it's collide when sphere touch it's border.I also check with default radius but the same situation occur.
Trigger
The scripting system can detect when collisions occur and initiate actions using the OnCollisionEnter function. However, you can also use the physics engine simply to detect when one collider enters the space of another without creating a collision. A collider configured as a Trigger (using the Is Trigger property) does not behave as a solid object and will simply allow other colliders to pass through. When a collider enters its space, a trigger will call the OnTriggerEnter function on the trigger object’s scripts.
http://docs.unity3d.com/Manual/CollidersOverview.html
You need to close isTrigger and use OnCollisionEnter(...).
For example this is my script of wood(which is below ). A smaller wood will be going down.
This is the beginning of my game
If I set isTrigger option true, onCollisionEnter2D function doesn't work. I need isTriggerEnter() method instead. it goes through my wood layer.
But if I set isTrigger false onCollisionEnter2D will be working. It will stay on my wood layer.