I am creating a board game and the character (the gameobject) has to move to the position of the pressed button.
public void TaskOnClick(Button btnpro)
{
int currentId;
int id = System.Int32.Parse(btnpro.name.Remove(0, 12));
if (currentButton == null)
{
currentId = 0;
} else
{
currentId = System.Int32.Parse(currentButton.name.Remove(0, 12));
}
if (currentId + 1 == id || currentId - 1 == id || currentId + 8 == id || currentId - 8 ==id)
{
Debug.Log("This should work");
Plane plane = new Plane(Vector3.up, transform.position);
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
float point = 0f;
if (plane.Raycast(ray, out point))
targetPosition = ray.GetPoint(point);
isMoving = true;
currentButton = btnpro;
}
}
This function is called when the player clicks the UI Button. Debug.Log() function runs so all the code here should work
And there is another function named MovePlayer() which is called in the Update() void. Here is the code of MovePlayer()
public void MovePlayer()
{
this.transform.LookAt(targetPosition);
this.transform.position = Vector3.MoveTowards(this.transform.position, targetPosition, speed * Time.deltaTime);
if (this.transform.position == targetPosition)
isMoving = false;
}
The problem is that the character doesnt move.
All the ui buttons call the function TaskOnClick here https://i.gyazo.com/8f57e9b37aa9cf7da743caaf03dc4f9d.png
I tried a lot of things and I didnt manage you make it work. If you know a better way to do what i want or you know what’s failing, let me know