Hi All,
So i suppose I’m missing something very basic here but hopefully you can help me out.
I have created a Cube from Unity’s menu of GameObject->Create Other->Cube and placed it in my world. I’ve also imported an FBX Mesh and attached a box collider (since a box more accurately represents my mesh shape than a capsule from the Character Controller).
In the script attached to my Player object (which contains the FBX Mesh, and Box Collider), i have the following in my update loop:
void Update() {
//very simple test gravity
transform.Translate(0, -5, 0);
}
Of course this causes my player to just go through the cube and just keep traveling downwards forever.
My question is basically, how can i make sure my Player will stay on top of the cube but if I go off of it, then i’ll fall down?
Any help would be appreciated,
Thanks!
You should be able to achieve that without any custom scripts attached to your character controller. What exactly are you trying to accomplish?
I basically just want the Box Collider around my Mesh to not fall through the Box Collider around my floor cube.
I don’t have a character controller because character controllers only have Capsule Colliders
Well, as long as the box collider is above the other one, it won’t fall through.
If you don’t want to use a CharacterController, you should think about attaching a Rigidbody instead.
Make sure that “Is Trigger” is not checked your colliders as well.
Yeah so that’s not what’s happening, it is falling through the floor. IsTrigger is not checked on both.
Basically i have two Box Colliders. No scripts attached to either. I attach a script to one that will on each update move it along it’s y position.
Do I have to handle the collision and negate this y movement? If so, how would I do that in my script?
I assume you are moving it down the Y axis to emulate gravity. This is not necessary as attaching a rigidbody component to the character cube will make it react to gravity and any forces you apply. The colliders attached let them collide with each other. Again, you don’t need to attach any scripts to emulate gravity.
Right so the problem with that is that my character is now controlled through the physics engine. And to make it move i need to apply forces and hope that it moves in the right direction.
I really just want to be able to have a character run around and jump to different platforms. The character should fall down the gaps but we be able to stay on top of the platforms.
When i add a rigidbody to my character it falls correctly and hits the platform. But then it just jitters and rotates every so slightly. So i set it to Kinematic, and do a manual gravity update and it goes right through the platform.
The reason your are having all these problems is because you are not using a character controller (these were designed to be the best option for creating characters). If you want to use a box collider because your character is box shaped, all you have to do is remove the capsule collider and add a box collider in it’s place.
Ok cool, so if I attach a CharacterCollider things start to work. How would I go about removing the capsule collider and replacing with a box collider? There is no capsule collider in the inspector.
I’m really sorry. Looking at the prefab again I realized that it doesn’t use a capsule collider (I didn’t check before I suggested it), so you can’t remove it. However, you can probably still attach a box collider and still achieve the effect you want. Modifying one of the character prefabs will most likely get you better and faster results than trying to create a cuboidal one from scratch.
What is your character, and what kind of movement and collisions do you want to achieve?
No worries, it just seems odd that I can’t change the collider on the CharacterController or make my own CharacterController out of a box collider.
My character is just a ship, like in FZero. I want it to be able to move forwards, turn left and right, and jump in air to land on different platforms. Pretty simplistic, but the basic setup of not falling through the floor with a box collider was killing me.
Your right, it seems like replacing should be possible, but I don’t know if it is or not. (I’ve only been using Unity since January, so what do I know?) However, if it is not possible, you could easily shrink the controller down and a box collider. However, I think that is a topic for a another thread =]
Try adding a rigidbody to the platform and turning the Is Kinematic option in the rigidbody on.
Ok so I solved it, but honestly, I’m a little disappointing in Unity’s lack of support for Colliders that aren’t Capsules when using the CharacterController.
What i did was add a RigidBody and BoxCollider to my Model. The RigidBody is set to isKinematic = true and useGravity = false. My Platforms are standard static box colliders.
In my Update loop, the Player is transform.Translate’d down according to my own manual gravity. When I get a trigger event of OnTriggerEnter, i set that value of gravity to be 0. OnTriggerExit, i set it to be back to the original amount.
It works exactly like the Character Controller (obviously minus alot of the extra functionality) and solves my immediate problem.
"In my Update loop, the Player is transform.Translate’d down according to my own manual gravity. "
Which is the problem - I’m sorry no one else noticed.
If your using physics, you must apply forces, and not translate the object.
@NPSF3000
No, i don’t want to use physics though. I want to have full control over the Player which is why I had to work around it this way.
Never mind, I see you’ve fixed it.