Terrain bug

Hello, I have a cube (the player)


With this movement script on it

   void FixedUpdate()
    {
       // Forwad MOVEMENT
        rdbody.AddForce(0, 0, forwardSpeed * Time.deltaTime);

       if (Input.GetKey("a") || Input.GetKey(KeyCode.LeftArrow))
       {
                  rdbody.AddForce(-sidewaysSpeed * Time.deltaTime, 0, 0, ForceMode.Force);
       }
       if (Input.GetKey("d") || Input.GetKey(KeyCode.RightArrow))
       {
          // transform.Translate(sidewaysSpeed * Time.deltaTime, 0, 0, 1);
          rdbody.AddForce(sidewaysSpeed * Time.deltaTime, 0, 0, ForceMode.Force);
       }
    }

and on this ground

with this physical material on it Imgur: The magic of the Internet

Everything is OK

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

BUT if I replace the ground with Unity Terrain
Imgur: The magic of the Internet Imgur: The magic of the Internet (the terrain has the same physical material)

This is what is happening


6727876--774352--upload_2021-1-15_12-37-27.png

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:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class SimpleMove : MonoBehaviour
{
    [SerializeField] private float forwardSpeed = 20;
    [SerializeField] private float sidewaysSpeed = 200;

    private Rigidbody rdbody;
  

    private void Start()
    {
        rdbody = GetComponent<Rigidbody>();
    }

    void FixedUpdate()
    {
        // Forwad MOVEMENT
        rdbody.AddForce(0, 0, forwardSpeed * Time.deltaTime, ForceMode.Force);

        if (Input.GetKey("a") || Input.GetKey(KeyCode.LeftArrow))
        {
            rdbody.AddForce(-sidewaysSpeed * Time.deltaTime, 0, 0, ForceMode.Force);
        }
        if (Input.GetKey("d") || Input.GetKey(KeyCode.RightArrow))
        {
            // transform.Translate(sidewaysSpeed * Time.deltaTime, 0, 0, 1);
            rdbody.AddForce(sidewaysSpeed * Time.deltaTime, 0, 0, ForceMode.Force);
        }
    }
}

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).

but if I freeze some of the rotation it’s killing my game :frowning: (making it not fun at all)

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?

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…

Well the game is avoiding obstacles and jump on ramps and etc. and if you freeze rotation its very boring

With other collision detections it’s bugging too… in a little bit different way but basically the same

I wanted for my next levels to use terrain because I will be able to make hills and holes and other fun stuff