0
\$\begingroup\$

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;               

    detectRoad();
}
private void detectRoad()
{
    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:

(Image) this animated gif about what i want

\$\endgroup\$
  • \$\begingroup\$ 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. \$\endgroup\$ – DMGregory Jun 10 '17 at 22:12
0
\$\begingroup\$

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.

\$\endgroup\$
  • \$\begingroup\$ 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) \$\endgroup\$ – S J Jun 13 '17 at 0:00
  • \$\begingroup\$ Does your slope have collider(s) on it? \$\endgroup\$ – S. Tarık Çetin Jun 13 '17 at 7:08
  • \$\begingroup\$ 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 \$\endgroup\$ – S J Jun 13 '17 at 21:45
  • \$\begingroup\$ 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? \$\endgroup\$ – S. Tarık Çetin Jun 14 '17 at 8:26
  • \$\begingroup\$ OK, i mean a word "shaking" = vibrating . may be the code which i wrote conflicts with what you give me , \$\endgroup\$ – S J Jun 14 '17 at 20:24
0
\$\begingroup\$

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

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.