- Home /
my character is falling through the floor
I want to have a character made out of cubes and affected by gravity to be destroyed per cube when I drop a ball on it with a script to destroy everything it touches. I want to let the character move too.
I have a character made out of different cubes parented to an empty gameobject with a rigidbody. all the cubes have a box collider. I have given all the cubes a kinematic rigidbody which let the cubes be destroyed seperately if I toggle off the gravity of the empty game object. But when I toggle it on my character falls through the floor.
Is there a way to not let the character fall through the floor?
Thanks in advance.
Just really quick, the reason he is falling through the floor is the kinematics. I will give you an answer with proper documentation and ways to understand or fix this problem.
Answer by SeaLeviathan · Jul 09, 2017 at 03:02 PM
Ok, I did some testing and I think I found a way to answer your question. When force is applied to a kinematic object, like a player falling due to gravity, the kinematic object ( to put it simply) can be interacted with, but does not interact with the world. That might be confusing, here's a link to the manual : https://docs.unity3d.com/Manual/class-Rigidbody.html You will just want to look at isKinematic. Basically it's used for interacting with objects with collision, but without moving them (Like the doc says, moving platforms)
This answers page has a pretty good explanation of kinematic bodies too: http://answers.unity3d.com/questions/580710/iskinematic-rigidbody-collisions.html (Look for the answer in like 20pt font)
You still have the question about making your character NOT fall through the floor, and I also understand where your need for kinematic rigidbody comes from, so I will suggest one of two things.
Try to raycast collisions (This is kind of difficult and sometimes not too reliable)
Make a gameobject for the feet of your player, position it correctly and then take off the mesh renderer and set its box collider to isTrigger. Then you can make a basic script for detecting collisions with that. ( I have a script like this, so if you can't figure that out I can help you there too)
Maybe this helped or it was just confusing, either way hope you can figure it out!
I tried to do the second one but I am stuck at the script part. I made a script using OnCollisionEnter and OnCollisionExit to check if the box hits the ground or not. I checked if it worked with a Debug.Log message but I did not recieve a message when the boxes hit the ground. I am also thinking of letting the character stop by activating the Is $$anonymous$$inamatic of the empty gameobject but i don't know if that is going to have consequences for if I want to move it.
Like I said I can help you with the script part for 2.
This is what I have, ill paste the important parts and explain through it. You are going to want to have the feet object parented to the player object, so it stays with you.
Script called grounding:
public bool isGrounded;
private void Update()
{
}
private void OnTriggerStay(Collider other)
{
isGrounded = true;
}
private void OnTriggerExit(Collider other)
{
isGrounded = false;
}
This part is for just setting a public boolean from true to false, depending. You said you tried OnCollisionEnter and Exit, but if you set this feet gameobject to a trigger as I had suggested, there would be no collisions, so this is where OnTrigger comes into play. A section of my fpsman script:
private void jump()
{
grounding feet = GameObject.FindGameObjectWithTag("Feet").GetComponent<grounding>();
if(Input.Get$$anonymous$$ey("space") && feet.isGrounded)
{
rb.AddForce(0, jumpSpeed, 0, Force$$anonymous$$ode.Force );
}
this first part of the function is a reference to the first script, called grounding. to get a reference, you call the class of the script you want to get a variable from (In this case "grounding"), give it a name just like a variable, (I called it feet) and set it equal to whats shown above. The tag is something you set up in the unity editor, it's a really useful way of communicating between objects and groups of objects. In this case, on the bottom of my player, I made a cube that fit on my characters feet, and tagged it "feet" accordingly. The last part is just GetComponent. So in our case, we want to access the "grounding" class as to get our isGrounded variable. The addforce inside of the if statement is just another way of moving an object, you can use whatever method you like. (in my case, I used a public variable I made called jumpspeed in the addforce function).
Once you have done all of this, you are going to want to attach the grounding script to the feet object.
Hope this helped! $$anonymous$$essage again if you need more help still!
For some reason my character is still falling through the floor.
Answer by botboi360 · Nov 27, 2020 at 04:38 PM
Just add a capsule collider and make sure that the ground has a box collider