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.
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;
}
}