I’m trying to move a cursor gameObject around a plane using the WASD keys. I suspect I’m closing in on the method, but it messes up at certain camera angles.
var oldMoveTo = moveTo;
Vector3 moveDir = new Vector3(0,0,0);
if (Input.GetAxis("Vertical") > 0f) { moveDir = Camera.main.transform.forward; }
if (Input.GetAxis("Vertical") < 0f) { moveDir = -Camera.main.transform.forward; }
if (Input.GetAxis("Horizontal") < 0f) { moveDir = -Camera.main.transform.right; }
if (Input.GetAxis("Horizontal") > 0f) { moveDir = Camera.main.transform.right; }
// this method to 'snap' it to a grid
if (moveDir.x < -0.3f) { moveTo.x = moveTo.x - 1; }
if (moveDir.x > 0.3f) { moveTo.x = moveTo.x + 1; }
if (moveDir.z < -0.3f) { moveTo.z = moveTo.z + 1; }
if (moveDir.z > 0.3f) { moveTo.z = moveTo.z - 1; }
if (moveTo != oldMoveTo) {
transform.position = Vector3.MoveTowards(transform.position, moveTo, 0.2f);
}
Can anyone tell me what I’m doing wrong? Thanks