How to prevent colliders from stopping each other while colliding - c#

I am working on a 3D unity project, which I have a platform in it and a character who runs on that platform, I placed the character on the platform but it started to fall down so I solved the problems by following these steps:
added a capsule collider to the object, it still fall through the platform
added a collider to the platform, then it didn't fall down but now these two objects are soled to each other so the character is just moving her legs in the same place,
Is there any way to make the character move on the platform while still using colliders?
Note 1: I made the collider trigger in the image to make the same exact behaviour with out colliders.
Note 2: I tried to put each one of them on a different layer but I get the same problem.
code:
void Update ()
{
rbody.velocity = new Vector3(rbody.velocity.x,0f,playerVelocity*Time.deltaTime);
rbody.transform.rotation = Quaternion.identity;
.
.
.
}

Making the collider trigger is like adding no collider. Trigger is only used when you want the collider to detect something but not stop it from running into things. So I believe you should add a non-trigger collider to both the character and the platform but make sure that the colliders are not intersecting when you put the character manually at first. If that didn't work, tell me exactly the coordinates of the character and the platform and the sizes of their colliders and I will try to replicate your scene to figure out the problem. Hope this helps!

Hi Friend you have 2 options...
Change the mass of your RigidBody
Use PhysicsMaterial and reduce the friction between the objects (Add a Physic Material to your Platform)
Increase the force and movement speed in your script.

Related

How do i make my player move with a moving platform in unity 2d

I'm making a 2D platformer and decided to add sticky platforms. I've made the platforms move, but the player doesn't move with them.
However, after parenting the player to the platform, the player still falls through. I have added two BoxCollider2Ds and set one of them as a Trigger. None of the colliders have a RigidBody2D
public class StickyPlatform : MonoBehaviour
{
private void OnTriggerEnter2D(Collider2D collision)
{
if (collision.gameObject.name == "Player")
collision.gameObject.transform.SetParent(transform);
}
private void OnTriggerExit2D(Collider2D collision)
{
if (collision.gameObject.name == "Player")
collision.gameObject.transform.SetParent(null);
}
}
trigger does not stop object move through the box, you can try OnCollisionEnter.
Usually I would have a ground check and you can say if(isGrounded&&onPlatform) //move with platform
So it sounds you have two problems:
Player is falling through platform
Player isn't sticking to the platform
Falling through the floor is a super common bug, with many causes. Here is a checklist I found:
does Object have a Collider? If not, select Object
go to the top bar
components
physics
choose appropriate collider
(if Terrain, check the last tab, the little cog-wheel)
Note: mesh-collider may cause problems
Particularly, if both FallingObject and GroundObject have mesh-collider
Particularly, if the mesh is animated
To avoid mesh-collider, you can build an aproximate shape of your mesh from
several primitive colliders (in parent-, child- or sibling-GameObjects)
If you need a Mesh-collider no matter what, you can try to place additional primitive colliders where they won't be in the way to 'enforce' the collisions
Is the Object a Trigger? If so, select Object
find its Collider-Component (if Terrain, check the last tab, the little cog-wheel)
remove the check of 'IsTrigger'
Is the Collider placed well?
Fiddle with center, size and skin-width (start with 0.1) until the green outline aproximately fits the character
(If you get really strange values, it might be due to scale (e.g. your mesh was way too big so you downsized to .01))
You may try to zero out all positioning (both unity and your modeling-program)
The link goes into even more cases.
Once the player can stay on, the physics engine should handle the momentum transfer to move the player with the platform using friction. Which again, requires RigidBody2D. I'm not sure why you're not using RigidBodys, it kind of feels like you're avoiding the solution.
Doing it this way should avoid the need to parent the player, and have a trigger volume as well, unless you want the player physically stuck and can't move on the platform.

Unity C# capsule collider and rigid body don't trigger using transform.position to move

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){}

unity2d create a one side collider

I have a maplestory game demo
I need character can jump through a collider from one side
the collider in my character and ground are like this:
character:capsulecollider2d
ground:polygoncollider2d
so the character can stand on the ground
also can jump on the ground
but now I want character jump from bottom of the ground,it cannot work
so, How can I deal this problem? Is there a configuration that can make collider only have one side?
-------------------------------------2017 08 11-------------------------------------
thanks a lot every one
thank you #EmreE #Jeroen De Clercq
I resolved this problem by using Platform Effector 2D provided by #EmreE
first: add platform effector 2d on my ground
and select "use one way"
second:select "Used by Effector"
now it works :)
Put your floor in a layer;
put your platforms in a layer;
When you jump remove collision with platform layer until u have reached a direction that points towards the floor.
When you press down key remove collision with platforms until you have passed one platform layer.
More information here ; https://docs.unity3d.com/ScriptReference/Physics.IgnoreLayerCollision.html

How to add physics to an object in Unity 5

I'm having a problem with Physics! I have imported an object which a bridge from 3ds max to unity 5 ,but the problem is that whenever I want to walk through on it ,I just fall down. It's like there's nothing called BRIDGE! I know there must be a problem about Physics. But how to fix it ?!
When importing a model into Unity it does not have any Collider.
Even though using auto-generated "MeshColliders" is an option
I highly recommend not to use them.
You never need such highly detailed colliders in a game.
Instead, you have to
Add the colliders manually
Here is a little step by step guide
This is the imported model (made with blender) without any collider whatsoever
First thing to do is add a new box collider to the components
as shown in this gif
The next thing to do is scaling the box collider
by either entering the values manually or using the drag/GUI version like I do in the gif below
(you probably know that already but the mode can be changed to orthographic by licking the small cube in between the cones in the upper right)
If necessary you can add as many "detail" collider as you want
by repeating the steps. Sometimes sphere or capsule colliders might fit better but keep in mind that they have a higher resolution!
In this case i added another box collider representing the upper part of the car:
Thats all you need to make things collide with your object
Right now you only have a MeshFilter, MeshRenderer, and Animator component attached. You walk through it because the mesh is only being rendered. In order to add collision, add a MeshCollider component to it.
Joe offers another way to do it with box colliders. There is a bit to know about both methods:
Box colliders will take more time to set up (rather than just adding a
component) and will have less precision. They will have faster performance.
Mesh colliders do not collide well with other mesh colliders.
However, this is often not an issue. If a mesh collider has the
"convex" checkbox checked then it will collide just fine with other
mesh colliders. Small items and any convex-shaped meshes should have
"convex" checked.
For something like your bridge.. it depends on your game. If you are doing a top-down game with limited mobility (no jumping) then box colliders may work for you. If you are making a first-person game then I would strongly suggest mesh colliders. If you decide to use box colliders then you must set them up carefully. Otherwise, physics may not match up with what the player sees!
Do not worry about performance at this time. Worry about it later IF it becomes an issue. With the physics upgrade in Unity 5, it probably won't be.

Somethings wrong with my colliders (Unity Game Engine)

I have a character, that has a character controller attached to it I am using transform.position, transform.forward and transform.rotate to move with the 'wasd' keys, that all works fine.
However it will not collide with any sort of colliders, whether is be box colliders or mesh colliders. trigger works on my character, i.e. it can trigger an object to destroy if it passes through, but the colliders that are not set to triggers (like walls), don't block the player, the player just goes through them.
Any ideas?
Note: using C#, unity free version 4.5.1
You Should not use transform for colliding.
Add ridged body and get reference to ridgedbody.
Then use reference.addForce
Guys i figured out the problem, as the previous answer said i should not use transform with character controller, however there's no need to change any colliders, i still kept my character controller on my player, but rather i used controller.SimpleMove to move the character, that fixes the problem and i get to keep the character controller on my object (no need to add extra colliders or rigidbody to the player).
http://docs.unity3d.com/ScriptReference/CharacterController.SimpleMove.html

Categories