Rigidbody movement with grid graph

Hi guys,

I’m trying to get a rigidbody character moving properly with a grid graph. I modified the AIPath script so that the rigidbody maintains a constant speed (I copied the controller from the rigidbody character controller on the Unity wiki and plugged it into the script in place of rigid.AddForce (dir). My character (an insect) moves very nicely as long as there’s no target. The moment I add a target, the character begins to turn and move erratically. It stops moving, then moves again, often in the X direction instead of forward along the Z axis. I can’t seem to get to the bottom of the problem. I’m wondering if it might have to do with the fact that I’m using forces to move the rigidbody forward, but using the Slerp rotation in the AI path script to turn the character to the new path segment. I’m posting the rigidbody controller below. Any help or suggestions as to how to incorporate it properly into the AIPath script would be greatly appreciated. Ideally, the character will move along the Z axis only, turning toward the new path segment smoothly. I apologize if I’m asking a lot. I’ve scoured the forums and tried countless solutions over a period of months with little success. The reason I’m using a rigidbody is that the character is a multipedal and must remain grounded on all of its legs (picture a spider moving along the ground). Thanks again!

BTW, I took out the Inputs when I plugged it AIPath script. I’m not using it as a character controller. I just wanted to incorporate the speed controller aspect of the script.

`using UnityEngine;
using System.Collections;

[RequireComponent (typeof (Rigidbody))]
[RequireComponent (typeof (CapsuleCollider))]

public class CharacterControls : MonoBehaviour {

public float speed = 10.0f;
public float gravity = 10.0f;
public float maxVelocityChange = 10.0f;
public bool canJump = true;
public float jumpHeight = 2.0f;
private bool grounded = false;



void Awake () {
    rigidbody.freezeRotation = true;
    rigidbody.useGravity = false;
}

void FixedUpdate () {
    if (grounded) {
        // Calculate how fast we should be moving
        Vector3 targetVelocity = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
        targetVelocity = transform.TransformDirection(targetVelocity);
        targetVelocity *= speed;

        // Apply a force that attempts to reach our target velocity
        Vector3 velocity = rigidbody.velocity;
        Vector3 velocityChange = (targetVelocity - velocity);
        velocityChange.x = Mathf.Clamp(velocityChange.x, -maxVelocityChange, maxVelocityChange);
        velocityChange.z = Mathf.Clamp(velocityChange.z, -maxVelocityChange, maxVelocityChange);
        velocityChange.y = 0;
        rigidbody.AddForce(velocityChange, ForceMode.VelocityChange);

        // Jump
        if (canJump && Input.GetButton("Jump")) {
            rigidbody.velocity = new Vector3(velocity.x, CalculateJumpVerticalSpeed(), velocity.z);
        }
    }

    // We apply gravity manually for more tuning control
    rigidbody.AddForce(new Vector3 (0, -gravity * rigidbody.mass, 0));

    grounded = false;
}

void OnCollisionStay () {
    grounded = true;    
}

float CalculateJumpVerticalSpeed () {
    // From the jump height and gravity we deduce the upwards speed 
    // for the character to reach at the apex.
    return Mathf.Sqrt(2 * jumpHeight * gravity);
}

}`