- Home /
Getting nowhere with my click to move script
I'm trying to make a script that handles the movement of a unit. I want to be able to select the unit, and then left click again to tell it where to move (I also want to restrict how far the unit can move based on movement points, but that will be a problem for another time.). I also have a UI element telling the unit when the movement phase is active. So far I have the ability to select the unit, let it know what phase the game is in, and register where the mouse clicked. Oh, and I don't want the script to be attached to the unit, as the plan is for it to handle the movement of all the players units.
The problem here is that the unit moves when it is selected AND the game is in the movement phase. Also when clicking in world space the unit only moves a bit and then resets and moves to the same coordinates on the next click.
public class Movement : MonoBehaviour { Mousemanager mousemanager; GameManager gm;
RaycastHit hit;
public float speed; //This should maybe be a variable in unit, so that different units have different speeds (should also be different in case of a charge)
Vector3 difference;
Vector3 oldDifference;
Vector3 currentPos;
// Use this for initialization
void Start()
{
speed = 5f;
mousemanager = GameObject.FindObjectOfType<Mousemanager>();
gm = GameObject.FindObjectOfType<GameManager>();
}
// Update is called once per frame
void Update()
{
//Click to set target for unit to move to
if (mousemanager.somethingSelected == true && gm.currentPhase == 1)
{
//Calculate how far the mousepointer is from the selected unit (based on local y and z)
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
if (Physics.Raycast(ray, out hit))
{
//if (Input.GetMouseButtonUp(0))
//{
// mousemanager.selectedObject.transform.localPosition = Vector3.Lerp(mousemanager.selectedObject.transform.position, hit.point, speed * Time.deltaTime).normalized;
//}
Moving();
//store difference in a variable and combare it to new difference. If they are the same, don't print it again
//make a UI element that follows the mouse pointer here, and tell the player the difference
}
//Show this compared to maxMove (maxMove - distance from unit to mousepointer)
}
}
//Unit moves to target
public void Moving()
{
if (Input.GetMouseButtonUp(0))
{
mousemanager.selectedObject.transform.localPosition = Vector3.Lerp(mousemanager.selectedObject.transform.position, hit.point, speed * Time.deltaTime);
//mousemanager.selectedObject.transform.position = Vector3.MoveTowards(currentPos, hit.point, speed * Time.deltaTime);
if (mousemanager.selectedObject.transform.position.y < 0.5f)
{
currentPos.y++;
}
Debug.Log("Ready to move");
}
}
}
Please help as I'm getting nowhere here.
Your answer
![](https://koobas.hobune.stream/wayback/20220612164047im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
RTS Minimap Movement 0 Answers
Space Game - Click To Move and Mouse Look 1 Answer
Idle Animation and Hold to Move 0 Answers
Struggling to smoothly disable diagonal movement 1 Answer
Rotation relativ animation paramter 0 Answers