# Fixed distance between character and ground whatever slope value

I am working on character moving on any ground meaning whatever slope degree ,the result i got is the character does not moving on just shake in his place the following the code i wrote

public float speed = 1;
bool isMoving;
Transform childPosition;
Rigidbody rb;

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

// Update is called once per frame
void FixedUpdate () {

if (Input.GetAxis("Horizontal") > 0)
{
rb.velocity = new Vector3(1f, rb.velocity.y, rb.velocity.z);
}
else
if (Input.GetAxis("Horizontal") < 0)
{
rb.velocity = new Vector3(-1f, rb.velocity.y, rb.velocity.z);
}
else
transform.position += transform.forward * Time.deltaTime * speed;

}
{
Ray ray = new Ray(transform.position, -transform.up);
RaycastHit hit;

if (Physics.Raycast(ray, out hit))
{
transform.rotation = Quaternion.FromToRotation(transform.up, hit.normal) * transform.rotation;
}
if (hit.distance < 0.6f)
{
transform.position += transform.up * Time.deltaTime;
}
else
if (hit.distance > 2f)
{
transform.position += -transform.up * Time.deltaTime;
}
}



This animated gif is about what I want: • Showing an animated gif or recording of your problem might help clarify it a bit. One thing I can say from looking at your code is that you should generally avoid moving a rigidbody with transform.position / rotation - stick to controlling it with velocity & angularvelocity if you want physics interactions with the body to behave correctly, or fall back on rigidbody.MovePosition/MoveRotation if you really need direct control - this will play nicer with the physics and won't break interpolation. – DMGregory Jun 10 '17 at 22:12

To be able to keep your distance fixed from the ground, you can do something like this:

//Pseudocode (this means it won't work if you copy and paste,
//implement the correct code on your own using this idea)

public float _fixedDist;

void Update
{
var hitinfo;
Raycast(from transform.position, towards vector3.down, out hitinfo);
var hitpoint = hitinfo.point;
var desiredY = hitpoint.y + _fixedDist;
transform.y = desiredY;
}


Note:

Do not modify Transform of a GameObject if it has a Rigidbody component on it. Instead you can set the position or rotation through Rigidbody component.

This is because, when you modify the Transform of a Rigidbody attached GameObject, the physics engine must "recalculate" the colliders, which is an expensive operation.

Also, inside FixedUpdate, you should use fixedDeltaTime instead of deltaTime.

• thank you it's working little bit, i followed your instructions but still there's problem it is working smoothly with "plane" its slope is zero but not with any slope degree (regression degree) – S J Jun 13 '17 at 0:00
• Does your slope have collider(s) on it? – S. Tarık Çetin Jun 13 '17 at 7:08
• Yes it does ,It has ( mesh collider ) but when the player detect (with Raycast) hit.point it does not see the collider ,so i put collider under the slope to test the code however the player still shaking in case of movement and shaking increases with high slope degree until come out of the slope – S J Jun 13 '17 at 21:45
• Sorry I can't understand what you mean by "shaking". Also, this code should detect any colliders. Can you try this on a new fresh project? – S. Tarık Çetin Jun 14 '17 at 8:26
• OK, i mean a word "shaking" = vibrating . may be the code which i wrote conflicts with what you give me , – S J Jun 14 '17 at 20:24

Thanks for everyone who tried to help me I found the answer how to make Fixed distance between character and ground the following is the answer :

Ray ray = new Ray(transform.position,-transform.up);
RaycastHit hit;
if (Physics.Raycast(ray, out hit)) {
transform.rotation = Quaternion.FromToRotation(transform.up,hit.normal)*transform.rotation;
targetDir = (transform.position - hit.point).normalized;
transform.position = hit.point +  targetDir* 1f;
}
`

that's worked with me ,i hope that's working with each one facing this problem