- Home /
Movement coroutine not working correctly
I have a coroutine setup up for making units in my RTS game move to a right mouse click but it is not working correctly. If I right click too soon after right clicking for the first time the unit will stop and it doesn't move at a consistent rate, it will slow down as it gets closer to the destination.
Ideally I would have the unit move at a consistent rate from it's starting point to the clicked point and have it able to change the destination while it is moving. Below is my current code
using UnityEngine;
using System.Collections;
public class UnitControl : MonoBehaviour {
public GameObject[] Units;
// Use this for initialization
void Start ()
{
}
bool unitMoving = false;
IEnumerator Move(GameObject Unit, Vector3 target)
{
while(Vector3.Distance(transform.position, target)>0.005f)
{
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit = new RaycastHit();
transform.position=Vector3.Lerp(transform.position, target, 3.0f*Time.deltaTime);
yield return null;
}
unitMoving = false;
}
// Update is called once per frame
void FixedUpdate ()
{
float moveSpeed = 5.1f;
if (Input.GetMouseButtonDown (1))
{
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit = new RaycastHit();
if (Physics.Raycast (ray, out hit))
{
Vector3 movePos = new Vector3 (hit.point.x, 1.5f, hit.point.z);
isSelectedScript isSelectedScript = GetComponent<isSelectedScript>();
Debug.Log("Debug working before isSelected loop");
if (isSelectedScript.isSelected == 1)
{
Debug.Log("This is between the if and the while loops");
transform.LookAt(hit.point);
StartCoroutine(Move(gameObject, hit.point));
// while(Vector3.Distance(transform.position, hit.point)>0.005f)
// {
// transform.position=Vector3.Lerp(transform.position, hit.point, 10.0f*Time.fixedDeltaTime);
// }
}
}
}
}
}
You are moving from the current position to the new position with a certain percentage, but since that distance gets smaller over time, so does the move distance. You have to store the original position and lerp between the original and the target.
To move to a new target if you clicked again, store the new current position, stop the current coroutine and start a new one.
So I need to store the starting position and target posotion as variables and then lerp between those two variables?
Alright, I implemented a stop coroutine command but I'm not sure if it is placed correctly
using UnityEngine;
using System.Collections;
public class UnitControl : $$anonymous$$onoBehaviour {
public GameObject[] Units;
// Use this for initialization
void Start ()
{
}
bool unit$$anonymous$$oving = false;
IEnumerator $$anonymous$$ove(GameObject Unit, Vector3 target)
{
Vector3 startPosition = transform.position;
while(Vector3.Distance(startPosition, target)>0.005f)
{
transform.position=Vector3.$$anonymous$$oveTowards(transform.position, target, 7*Time.deltaTime);
yield return null;
}
}
// Update is called once per frame
void FixedUpdate ()
{
float moveSpeed = 5.1f;
if (Input.Get$$anonymous$$ouseButtonDown (1))
{
StopCoroutine("$$anonymous$$ove");
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit = new RaycastHit();
if (Physics.Raycast (ray, out hit))
{
Vector3 movePos = new Vector3 (hit.point.x, 1.5f, hit.point.z);
isSelectedScript selectedScript = GetComponent<isSelectedScript>();
Debug.Log("Debug working before isSelected loop");
if (selectedScript.isSelected == 1)
{
Debug.Log("This is between the if and the while loops");
transform.LookAt(hit.point);
StartCoroutine($$anonymous$$ove(gameObject, hit.point));
}
}
}
}
}
Answer by fafase · Mar 02, 2015 at 01:42 PM
Use MoveTowards instead of Lerp. Lerp uses the remaining distance while MoveTowards uses the distance you give as third parameter.
As a result, the movement is constant.
If the distance given is greater than the remaining distance then it returns the target position.
I changed it to move towards and incorporated the suggestion above but now the unit does not move. This is the new coroutine
IEnumerator $$anonymous$$ove(GameObject Unit, Vector3 target)
{
Vector3 startPosition = transform.position;
while(Vector3.Distance(startPosition, target)>0.005f)
{
transform.position=Vector3.$$anonymous$$oveTowards(startPosition, target, 7.0f*Time.deltaTime);
yield return null;
}
}
This script is applied directly onto the unit
Alright I changed that and the movement is constant for the first right click but after that it gets gradually slower with each successive right click until it wont move at all
Could your issue be that you have multiple coroutines running at the same time and they just do it totally wrong, somehow canceling the effect of the previous ones?
While one is on, you should not be able to trigger a new one or you have to first cancel if any already running.
Also, avoid this:
isSelectedScript isSelectedScript = GetComponent<isSelectedScript>();
your variable name is the same as the type. It seems to go but is not a good idea.
Apparently my comment didn't post so I am going to post it again. I tried that and added in a stop coroutine but it still isnt working.
using UnityEngine;
using System.Collections;
public class UnitControl : $$anonymous$$onoBehaviour {
public GameObject[] Units;
// Use this for initialization
void Start ()
{
}
bool unit$$anonymous$$oving = false;
IEnumerator $$anonymous$$ove(GameObject Unit, Vector3 target)
{
Vector3 startPosition = transform.position;
while(Vector3.Distance(startPosition, target)>0.005f)
{
transform.position=Vector3.$$anonymous$$oveTowards(transform.position, target, 7*Time.deltaTime);
yield return null;
}
}
// Update is called once per frame
void FixedUpdate ()
{
float moveSpeed = 5.1f;
if (Input.Get$$anonymous$$ouseButtonDown (1))
{
StopCoroutine("$$anonymous$$ove");
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit = new RaycastHit();
if (Physics.Raycast (ray, out hit))
{
Vector3 movePos = new Vector3 (hit.point.x, 1.5f, hit.point.z);
isSelectedScript selectedScript = GetComponent<isSelectedScript>();
Debug.Log("Debug working before isSelected loop");
if (selectedScript.isSelected == 1)
{
Debug.Log("This is between the if and the while loops");
transform.LookAt(hit.point);
StartCoroutine($$anonymous$$ove(gameObject, hit.point));
}
}
}
}
}