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:
Enable the rigidbody Freeze Rotation constraints.
Position your cube slightly above the terrain (0.001 is fine) and enable the Freeze Position constraint for y.
Use a capsule collider (the rounded edges are more forgiving) (you may need other constraints / controllers for stability depending on what else it hits).
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…