Search Unity

Terrain bug

Discussion in 'Physics' started by SonGokuBg, Jan 15, 2021.

  1. SonGokuBg

    SonGokuBg

    Joined:
    Apr 16, 2020
    Posts:
    146
    Hello, I have a cube (the player)
    upload_2021-1-15_12-21-7.png
    upload_2021-1-15_12-25-22.png

    With this movement script on it
    Code (CSharp):
    1.    void FixedUpdate()
    2.     {
    3.        // Forwad MOVEMENT
    4.         rdbody.AddForce(0, 0, forwardSpeed * Time.deltaTime);
    5.  
    6.        if (Input.GetKey("a") || Input.GetKey(KeyCode.LeftArrow))
    7.        {
    8.                   rdbody.AddForce(-sidewaysSpeed * Time.deltaTime, 0, 0, ForceMode.Force);
    9.        }
    10.        if (Input.GetKey("d") || Input.GetKey(KeyCode.RightArrow))
    11.        {
    12.           // transform.Translate(sidewaysSpeed * Time.deltaTime, 0, 0, 1);
    13.           rdbody.AddForce(sidewaysSpeed * Time.deltaTime, 0, 0, ForceMode.Force);
    14.        }
    15.     }
    and on this ground
    upload_2021-1-15_12-36-8.png

    with this physical material on it https://imgur.com/a/gOASdPe

    Everything is OK
    (I am moving the cube left and right with the arrows)

    BUT if I replace the ground with Unity Terrain
    https://imgur.com/a/driuww2 https://imgur.com/a/FBJxgzf (the terrain has the same physical material)

    This is what is happening
     

    Attached Files:

  2. AlTheSlacker

    AlTheSlacker

    Joined:
    Jun 12, 2017
    Posts:
    326
    I think the problem is coming from the corners of the box collider making contact with the terrain due to floating point precision. I was able to replicate your problem using the following movement script:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class SimpleMove : MonoBehaviour
    6. {
    7.     [SerializeField] private float forwardSpeed = 20;
    8.     [SerializeField] private float sidewaysSpeed = 200;
    9.  
    10.     private Rigidbody rdbody;
    11.  
    12.  
    13.     private void Start()
    14.     {
    15.         rdbody = GetComponent<Rigidbody>();
    16.     }
    17.  
    18.     void FixedUpdate()
    19.     {
    20.         // Forwad MOVEMENT
    21.         rdbody.AddForce(0, 0, forwardSpeed * Time.deltaTime, ForceMode.Force);
    22.  
    23.         if (Input.GetKey("a") || Input.GetKey(KeyCode.LeftArrow))
    24.         {
    25.             rdbody.AddForce(-sidewaysSpeed * Time.deltaTime, 0, 0, ForceMode.Force);
    26.         }
    27.         if (Input.GetKey("d") || Input.GetKey(KeyCode.RightArrow))
    28.         {
    29.             // transform.Translate(sidewaysSpeed * Time.deltaTime, 0, 0, 1);
    30.             rdbody.AddForce(sidewaysSpeed * Time.deltaTime, 0, 0, ForceMode.Force);
    31.         }
    32.     }
    33. }
    Which is just a full class for your FixedUpdate.

    Here are some options to prevent the problem:
    1) Enable the rigidbody Freeze Rotation constraints.
    2) Position your cube slightly above the terrain (0.001 is fine) and enable the Freeze Position constraint for y.
    3) Use a capsule collider (the rounded edges are more forgiving) (you may need other constraints / controllers for stability depending on what else it hits).
     
  3. SonGokuBg

    SonGokuBg

    Joined:
    Apr 16, 2020
    Posts:
    146
    but if I freeze some of the rotation it's killing my game :( (making it not fun at all)
     
  4. BakeMyCake

    BakeMyCake

    Joined:
    May 8, 2017
    Posts:
    175
    I was able to reproduce this aswell. If I disable the terrain collider and apply a regular box collider to the terrain it works again, but something tells me that's not a solution you're going to like.

    It also stops being like that if I switch to other collision detection modes, maybe it's a bug in continuous dynamic?
     
  5. AlTheSlacker

    AlTheSlacker

    Joined:
    Jun 12, 2017
    Posts:
    326
    You can get it in all detection modes, but dynamic appears worse (possibly because it predicts the off horizontal increasing?). I don't think it's a bug, more of a consequence of doing lots of floating point ops in the rb physics and inevitably getting some noise.

    Interesting that you say you don't see it with a box collider though... Perhaps there is an issue with the flatness of the terrain....

    It would be really helpful if the OP actually explained the implementation issues he has with each of my three suggestions and gave sufficient details of the issues / his game, that a work around could be suggested....
     
  6. SonGokuBg

    SonGokuBg

    Joined:
    Apr 16, 2020
    Posts:
    146
    Well the game is avoiding obstacles and jump on ramps and etc. and if you freeze rotation its very boring
     
  7. SonGokuBg

    SonGokuBg

    Joined:
    Apr 16, 2020
    Posts:
    146
    With other collision detections it's bugging too.. in a little bit different way but basically the same
     
  8. SonGokuBg

    SonGokuBg

    Joined:
    Apr 16, 2020
    Posts:
    146
    I wanted for my next levels to use terrain because I will be able to make hills and holes and other fun stuff