1
\$\begingroup\$

I have a player with a Rigidbody, I want it to follow along the Waypoints. I have used MoveTowards but it seems also not to work however. I want to use MovePosition to add force while grinding. I commented a code that could maybe work but I got an error on that, check //ADDFORCE lines.

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 FixedUpdate()
{
    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);
        //ADDFORCE
        /*
        Vector3 movePosition = transform.position;
        movePosition = Mathf.MoveTowards(transform.position, waypoints[currentWaypoint].transform.position * grindSpeed * Time.deltaTime);
        rb.MovePosition(movePosition);
        */
        //ROTATE
        var rotation = Quaternion.LookRotation(waypoints[currentWaypoint].transform.position - transform.position);
        transform.rotation = Quaternion.Slerp(transform.rotation, rotation, turnSpeed * Time.deltaTime);

        if(Mathf.Abs(transform.position.x - waypoints[currentWaypoint].transform.position.x) < 1
            && (Mathf.Abs (transform.position.y - waypoints[currentWaypoint].transform.position.y) < 1)
            && (Mathf.Abs (transform.position.z - waypoints[currentWaypoint].transform.position.z) < 1))
        {
            //rb.useGravity = false;
            currentWaypoint++;
        }
    }
}

void OnTriggerEnter(Collider other)
{
    if(other.gameObject.tag == "GrindWayPoint")
    {
        Debug.Log ("Waypoint!!");
        isGrinding = true;
    }
}
\$\endgroup\$
  • \$\begingroup\$ Do you want MovePosition, or AddForce? You mention both position and force but they're two different methods. \$\endgroup\$ – DMGregory Jan 26 '18 at 13:43
0
\$\begingroup\$

Your Vector3.MoveTowards and Rb.MoveTowards will works same. However you can use Vector3.Distance() instead of

if(Mathf.Abs(transform.position.x - waypoints[currentWaypoint].transform.position.x) < 1
        && (Mathf.Abs (transform.position.y - waypoints[currentWaypoint].transform.position.y) < 1)
        && (Mathf.Abs (transform.position.z - waypoints[currentWaypoint].transform.position.z) < 1))
    {
        //rb.useGravity = false;
        currentWaypoint++;
    }

You can use it like

Vector3.Distance(transform.position,waypoints[currentwaypoint].transfrom.positon)

It Will return float you can make a condition out of it. Also You are not considering trasnform.forward in your movement. Your player may not move using forward. You should check out this link.

https://docs.unity3d.com/ScriptReference/Rigidbody.MovePosition.html

| improve this answer | |
\$\endgroup\$
0
\$\begingroup\$

It looks like your code should work... bump. I do have a few tips however.

1) try to work in FixedUpdate when working with Rigidbodies.

2) Use movePosition to move Rigidbodies, rather than setting the position.

The above 2 tips will cause your game to jitter less. If you use fixedUpdate to move a rigidbody, it could move through the collider without being detected in the physicssteps.

3) anim.SetBool ("isOnGrinding", isGrinding); is 3 lines of code less than the ones you already have in your fixedUpdate.

4) Vector3.distance() is also shorter than calculating the offset between all 3 axis of a vector3 seperately.

5) When using moveTowards, don't multiply your targetPosition by a float, this would give you an entirely different position. if you want to move it gradually, use Vector3.Lerp, or calculate the offset first.

Try to provide a bit more information too, it seems like it should work, but i don't really know anything about your setup. Do your waypoints have colliders on them? Or does the gameObject with the waypoints have a collider?

| improve this answer | |
\$\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.