Cant stop Movetowards, already tried transform.position=end.position
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!
Answer by Baste · Nov 11, 2015 at 02:35 PM
It's because you have a ; at the end of the if-check.
So this:
if(transform.position == endPoint.position);
{
Debug.Log ("Stopped");
shouldMove = false;
}
Should be this:
if(transform.position == endPoint.position)
{
Debug.Log ("Stopped");
shouldMove = false;
}
There's two somewhat strange C# things going on here. First of all, ; is a valid empty statement on it's own. At the same time, you can wrap any set of statements in { }, which creates a local scope. You usually only do it to define what an if- or for-statement should work on, but it's completely valid to just do it whenever. So your code is doing this:
if(transform.position == endPoint.position)
; //Do nothing
{ // Bracket does not interact with if-statement
Debug.Log ("Stopped");
shouldMove = false;
}
Dropping the ';' should fix your problem.
Thankyou very much @Baste ! It's always something so small.
Your answer
![](https://koobas.hobune.stream/wayback/20220612044712im_/https://answers.unity.com/themes/thub/images/avi.jpg)