Search Unity

Detecting player landing on object not on ground

Discussion in 'Physics' started by Tempy111, Oct 21, 2021.

  1. Tempy111

    Tempy111

    Joined:
    Apr 26, 2021
    Posts:
    32
    Okay.. I've looked but i'm pretty bad at searching it seams..

    I have a area which is a prefab and a player (in 3D). Meshs in the prefab use Box Colliders and the player is a rigidbody (set as Kinematic cause it just falls though the ground if it isn't) which is inside group or whatever it's called, which has a Character controller attached, and has a Capsule Collider.

    anyway.. it interact with the ground fine. and i use the follow code to tell if it's in the air (to display falling animation) or landing on the ground (at which it displayes a landing animation before normal standing)

    if (Physics.Raycast(transform.position,Vector3.down,distToGround+0.1f)){
    animator.SetBool("isFalling", false);
    Grounded = false;
    animator.SetBool("isJumping", false);
    } else {
    animator.SetBool("isFalling", true);
    Grounded = true;
    }


    problem is, I've added a Box to the scene, so the player has something it can jump onto. While it does do that, the Raycast doesn't detect it's hit so it's stuck thinking it's falling, yet the colliders have hit and the character can walk about on top of it. Any ideas or what i have totally screwed up?
     
  2. Tempy111

    Tempy111

    Joined:
    Apr 26, 2021
    Posts:
    32
    issue is still driving mde mad and so-many so-called guides these day are just videos where people show you one way of doing something without comment or explaining anything... as if the art of making a tutorial is completely a lost art..
     
  3. arkano22

    arkano22

    Joined:
    Sep 20, 2012
    Posts:
    1,928
    My guess is the box does not have a collider component, or it's on a layer set to ignore raycasts.

    A kinematic rigidbody acts as if it had infinite mass: it's basically unaffected by physics. It really shouldn't be kinematic. If a non-kinematic rigidbody is falling trough the ground, chances are the ground has no collider (or, its thin enough to cause tunneling).
     
  4. Tempy111

    Tempy111

    Joined:
    Apr 26, 2021
    Posts:
    32
    Thanks..
    the Player character container thing has a capsule collider set up, and the box has a box Collider.

    the Ground has a box collider on it too which is 4... whatever units it's using by default.

    all objects so far are on the default Layer
     
  5. arkano22

    arkano22

    Joined:
    Sep 20, 2012
    Posts:
    1,928
    If both have a collider and are on the default layer, there's no clear reason I can think of that would cause the raycast to fail (given the info you've provided).

    How is "distToGround" being calculated in your code?

    Unity uses meters, as far as distance units goes. 1 unit = 1 meter. Kilograms for mass, in case you're wondering. Note these only make sense when dealing with the physics engine, otherwise its just "units".
     
  6. Tempy111

    Tempy111

    Joined:
    Apr 26, 2021
    Posts:
    32
    mm.. just looking.. I'm not quite sure how it's calculated.. in fact.. its... not? that's a big problem..
    It's defined: public float distToGround = 0.1f; but then the next time it's used is the Raycast line so it's only ever set as that pre-defined value.. which doesn't seam quite right to me.. I'm sure i followed the one guide i found which to be honest, doesn't seam to have been that useful to me..

    looks like something like.. distToGround = collider.bounds.extents.y; called in Start()?

    ah meters.. okay.. yeah.. most of the time, easily just to say 'units' ^_^ thanks
     
  7. arkano22

    arkano22

    Joined:
    Sep 20, 2012
    Posts:
    1,928
    Correct: the distance from the center of your player object (which is what transform.position refers to) to the bottom the player is half the height (Y axis is vertical) of its bounding box. Only need to set this value once at Start as it will never change unless you resize your character. Does this make sense to you?

    So in the raycast, the length of the ray is this distToGround value plus a small constant (0.1 meters), in order for the ray to be able to hit whatever is immediately below the player.

    This should work just fine, no idea why raycasting against the floor works but the box doesn't. Any other code in your script? could you post the entire thing?

    Also, have you tried debugging trough your code step by step and/or drawing the ray so that you can visually inspect what's happening?
     
  8. Tempy111

    Tempy111

    Joined:
    Apr 26, 2021
    Posts:
    32
    just zipped up the whole folder which should be enough.. probebly more then whats needed and i'm sure there are TONS of issues which could/should be done better..
    https://www.tempysart.com/F&F3D.zip
    that's probebly the easiest way to see what's what..

    thanks ^_^
     
  9. arkano22

    arkano22

    Joined:
    Sep 20, 2012
    Posts:
    1,928
    Hi,

    - Units are way off, your character is around 20 meters tall. This causes physics to look sluggish, a 20 meters tall thing looked from very far away seems to move much slower than a 1 meter object up close.

    Always use real-world units when dealing with physics. See "Use the right size" in Unity's manual: https://docs.unity3d.com/Manual/class-Rigidbody.html

    - You're using both a character controller and a capsule collider in your character. You don't need them both, both are colliders. Keep the character controller only.

    - The character controller is 2 meters tall and placed at the feet of the character, your capsule is 20 meters tall and placed at the center. As a result, the character controller sticks out the bottom of the character just enough for the raycast to be unable to actually hit anything (since the ray is only 1.1 meters long, which is shorter than your character).

    - You've added a rigidbody component to a child object of your character instead of the character itself. This is just...wrong.

    Right now the raycast should be the least of your worries. Pretty much everything in the project needs to be fixed before the raycast has a chance to work as intended.

    Looks to me like you're trying to get way ahead of yourself. Spend some time learning the basics first: watch some official tutorials, read the manual, download some sample projects and look at how they're made, etc.
     
  10. Tempy111

    Tempy111

    Joined:
    Apr 26, 2021
    Posts:
    32
    okay ^_^ thanks. didn't notice the character and everything was way over sized..
    I'll try to read through a bit more and see what happens.