I’d like the Movetowards code to stop when it arrives at the endPoint.
I added an if(transform.position == endPoint.position) however this just makes the movetowards stop after 1 frame - without ever reaching the endPoint.
Here’s my code (Move only occurs on a double click, so has a click counter first)
What am I doing wrong?! Thanks!
using UnityEngine;
using System.Collections;
public class MoveReset : MonoBehaviour {
int mouseClicks = 0;
float mouseTimer = 0f;
float mouseTimerLimit = .25f;
bool shouldMove;
bool shouldReturn;
public Transform startPoint;
public Transform endPoint;
public float speed;
Vector3 pos0;
void Update() {
float step = speed * Time.deltaTime; //Lerp Speed
if(Input.GetMouseButtonDown(0))
{
mouseClicks++;
}
if(mouseClicks >= 1 && mouseClicks < 3) //detects if a double click
{
mouseTimer += Time.fixedDeltaTime;
if(mouseClicks == 2)
{
if(mouseTimer - mouseTimerLimit < 0)
{
Debug.Log("Mouse Double Click");
mouseTimer = 0;
mouseClicks = 0;
checkpos(); //attempt to store orignal starting position
shouldMove = true;
shouldReturn = false;
}
}
if(mouseTimer > mouseTimerLimit)
{
Debug.Log("Single Click"); //single click does nothing yet
mouseClicks = 0;
mouseTimer = 0;
}
}
if (Input.GetMouseButtonDown (1)) { //returns to original position on right-click
Debug.Log ("Right Click");
shouldReturn = true;
shouldMove = false;
}
if(shouldMove) //move to new position
{
Debug.Log ("Moving");
transform.position = Vector3.MoveTowards(startPoint.position, endPoint.position, step);
}
if (shouldReturn) { //return to start position
Debug.Log ("Returning");
transform.position = Vector3.MoveTowards(startPoint.position, pos0, step);
}
if(transform.position == endPoint.position); //attempt to stop move ***NOT WORKING PROPERLY***
{
Debug.Log ("Stopped");
shouldMove = false;
}
}
void checkpos(){ //attempt to store orignal starting position
Debug.Log ("Checking");
pos0 = startPoint.localPosition;
}
}
EDIT: Adding a Debug.Log to also print the pos0 co-ordinates and the transform.position co-ordinates, it is detecting the differenec (i.e transform.position is NOT equal to pos0 - yet it changes the bool from true to false after 1 frame anyway!