So we are making this 2D game and the character doesnt go to the positive coordinated integer tiles.
This is our code:
using UnityEngine;
using System.Collections;
public class Clicktomove : MonoBehaviour
{
public float speed = 0f;
private Vector3 target;
void Start()
{
target = transform.position;
}
void Update()
{
if (Input.GetMouseButtonDown(0))
{
target = Camera.main.ScreenToWorldPoint(Input.mousePosition);
target.z = transform.position.z;
}
transform.position = Vector3.MoveTowards(transform.position, target, speed * Time.deltaTime);
Vector3 a = transform.position;
Vector3 b = new Vector3(Mathf.Floor(a.x), Mathf.Floor(a.y), Mathf.Floor(a.x));
transform.position = b;
}
}
public float speed = 0f;
private Vector3 target;
void Start()
{
target = transform.position;
}
void Update()
{
if (Input.GetMouseButtonDown(0))
{
target = Camera.main.ScreenToWorldPoint(Input.mousePosition);
target.z = transform.position.z;
Debug.Log(target);
}
Vector3 gridLockedTarget = new Vector3(Mathf.Floor(target.x),Mathf.Floor(target.y),transform.position.z);
transform.position = Vector3.MoveTowards(transform.position, gridLockedTarget, speed * Time.deltaTime);
//Vector3 a = transform.position;
//Vector3 b = new Vector3(Mathf.Floor(a.x), Mathf.Floor(a.y), Mathf.Floor(a.x));
//transform.position = b;
}
@Polyphia02 I did these modifications to your base code and now it works in all directions. The problem you were having was that after you called transform.MoveTowards, it would constantly try to update your player’s position to the new position, but each time floor it’s position, not letting it move at all in the positive axes. The reason as to why it works negative probably has to do with unity’s weird implementation of floor not working properly with negative numbers, but that’d just be a guess.
Basically, don’t try to move your player twice in the same frame, and try to avoid directly modifying transform data, other than that it’s pretty solid movement logic, I barely changed much. I hope that helps!