0
\$\begingroup\$

I have a player with a Rigidbody, I want it to follow along the Waypoints, I have a script for it I tested it first on a Cube, everything worked fine, but on my character it just moves Towards the first waypoint then stays on the first waypoint instead of going ahead. Even if I comment out the LookAt Code section its not working. It could maybe cause the Y position of the waypoints that my player is stuttering, I changed it now. It is now moving along but stuttering much.

Here is a picture of the problem: Picture of the problem Picture of the problem1

Here is my code:


public GameObject[] waypoints;

public float grindSpeed;
public float turnSpeed;

public int currentWaypoint;

private Animator anim;
private Rigidbody rb;

public bool isGrinding = false;

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


void Update()
{
    MoveAlongWaypoints();

    if(isGrinding)
        anim.SetBool ("isOnGrinding", true);
    else if(!isGrinding)
        anim.SetBool("isOnGrinding", false);
}

void MoveAlongWaypoints()
{
    if(isGrinding)
    {
        //TRANSLATE
        transform.position = Vector3.MoveTowards(transform.position, waypoints[currentWaypoint].transform.position, grindSpeed * Time.deltaTime);
        //ROTATE
        var rotation = Quaternion.LookRotation(waypoints[currentWaypoint].transform.position - transform.position);
        transform.rotation = Quaternion.Slerp(transform.rotation, rotation, turnSpeed * Time.deltaTime);

        if(transform.position == waypoints[currentWaypoint].transform.position)
        {
            currentWaypoint++;
        }
    }
}

void OnTriggerEnter(Collider other)
{
    if(other.gameObject.tag == "GrindWayPoint")
    {
        Debug.Log ("Waypoint!!");
        isGrinding = true;
    }
}
\$\endgroup\$
  • \$\begingroup\$ Be careful when mixing Rigidbody physics with direct transformation overrides. This rips control of the object's movement away from the physics engine, and can cause judder, weird collision behaviours, and even hamper performance (since the physics engine has to deal with out-of-band changes that are effectively teleports every time you move something without it). If you have a Rigidbody, route your movement through that, with velocity, AddForce, MovePosition, etc. \$\endgroup\$ – DMGregory Jan 25 '18 at 13:43
  • \$\begingroup\$ I know but how I move the player along the waypoints with AddForce or MovePosition? Never did this before like that. \$\endgroup\$ – Daniel Jan 25 '18 at 15:33
  • \$\begingroup\$ That sounds like something worth posting as a Question. ;) \$\endgroup\$ – DMGregory Jan 25 '18 at 16:21
  • \$\begingroup\$ I concur with DM...if you are using the physics engine then move via forces. That's what it was made for. \$\endgroup\$ – Dunk Jan 25 '18 at 21:08
0
\$\begingroup\$

Position is made up of floats. Most likely the 2 values in this check never equal each other because of roundoff errors:

// These will probably never equal each other if(transform.position == waypoints[currentWaypoint].transform.position)

When comparing floats you need to set some tolerance range to do the comparison and call it "good enough". e.g.

if((Math.Abs(transform.position.x - waypoints[currentWaypoint].transform.position.x) < 0.01) && (Math.Abs(transform.position.y - waypoints[currentWaypoint].transform.position.y) < 0.01) && (Math.Abs(transform.position.z - waypoints[currentWaypoint].transform.position.z) < 0.01)) { //close enough...assume they are equal...so do some stuff currentWaypoint++; }

\$\endgroup\$
  • \$\begingroup\$ You are right, what I tried would never equal, I changed now some code but I get an error when I try moving with rb.MovePosition. I get MoveTowards takes "2" Arguments, I tried converting MoveTowards to MovePosition but seems not to work. //ADDFORCE Vector3 movePosition = transform.position; movePosition = Mathf.MoveTowards(transform.position + waypoints[currentWaypoint].transform.position * grindSpeed * Time.deltaTime); rb.MovePosition(movePosition); \$\endgroup\$ – Daniel Jan 26 '18 at 7:07
  • \$\begingroup\$ Use: Vector3 moveDir = Vector3.Normalize(waypoints[currentWaypoint].transform.position - transform.position); rb.AddForce(DRIVING_FORCE*moveDir); Where DRIVING_FORCE can be hard-coded or calculated frame-by-frame to be more realistic. Start with hard-coding. NOTE: You'll need to do this calculation and call AddForce every frame because forces are zeroed on each update. \$\endgroup\$ – Dunk Jan 30 '18 at 17:00

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.