Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Player falls through the floor

Discussion in 'Physics' started by gogocook25, Oct 17, 2018.

  1. gogocook25

    gogocook25

    Joined:
    Sep 30, 2018
    Posts:
    36
    Hi everybody! I'm a newbie so please point out my misconceptions.

    The problem I am having is my player is sinking into the surface when I try to move it from point A to point B using RigidBody.MovePosition(newPosition). My surface consists of a grid of cubes with an initial height of zero (the idea is I am going to vary these heights). When I set the player gravity to 0 the player moves to the destination as expected. My player has a capsule collider & ridigbody and my cubes have box colliders (I'm not sure if they need rigidbody)

    I know I am probably omitting something important so I hope you don't mind if I paste my code below. Any help/hints are well appreciated.

    I am calling testWater() from my player controller's FixedUpdate method.

    Code (CSharp):
    1.     protected void testWater()
    2.     {
    3.         Transform personTransform = transform;
    4.         water = GameObject.FindWithTag("Water Level");
    5.         float personHeight = personTransform.position.y;
    6.         float headLevel = personCollider.height-0.3f; //.9f
    7.         Vector3 moveHere = new Vector3(2f, 0, 2f);
    8.         move(moveHere);
    9.     }
    10.  
    11.     private bool move(Vector3 target)
    12.     {
    13.         Vector3 start = transform.position;
    14.         transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(target - start), inverseRotationTime * Time.deltaTime);
    15.         personCollider.enabled = false;
    16.         bool path = Physics.Linecast(start, target);
    17.         if (path)
    18.         {
    19.             smoothMovement(target);
    20.             return true;
    21.         }
    22.         //we hit something
    23.         return false;
    24.     }
    25.  
    26.     private void smoothMovement(Vector3 end)
    27.     {
    28.         float sqrRemainingDistance = (transform.position - end).sqrMagnitude;
    29.         Debug.Log("square remaining distance is " + sqrRemainingDistance);
    30.         if (sqrRemainingDistance>.64f)
    31.         {
    32.  
    33.             Vector3 newPosition = Vector3.MoveTowards(rb.position, end, inverseMoveTime * Time.deltaTime);
    34.             rb.MovePosition(newPosition);
    35.             sqrRemainingDistance = (transform.position - end).sqrMagnitude;
    36.         }
    37.     }
    38.  
     
  2. gogocook25

    gogocook25

    Joined:
    Sep 30, 2018
    Posts:
    36
    i figured it out. I turned off the collider for the linecast and did not renable it