Ok. I have scratched my head long enough. I am having an ID10T error. I am trying to use Vector3.MoveTowards to move my character on a grid. My script works just as I expect it to work after the first move. The player moves smoothly on the X or Y axis from one grid cell to the next and always stops at exactly in the grid. However, on the first press of the movement keys the MoveToward is telling the player transform to move to the next cell. I have posted my feeble attempt at debugging this issue. As you can see I inserted a log before the MoveTowards and after. In frame 993, moves one full cell in the MoveTowards. I’m sure I am missing something simple, but I can’t figure it out. Any help would be appreciated.
Thanks
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.InputSystem;
public class PlayerController : MonoBehaviour
{
/* private const string IS_WALKING_SIDE = "IsWalkingSide";
private const string IS_WALKING_UP = "IsWalkingUp";
private const string IS_WALKING_DOWN = "IsWalkingDown";
private const int RIGHT = 1;
private const int LEFT = -1;
private const int UP = 1;
private const int DOWN = -1;*/
[SerializeField] private float _speed;
[SerializeField] private Transform _movePoint;
[SerializeField] private LayerMask _obstacles;
private PlayerControls _playerControls;
private Vector2 _playerMovement;
private Animator _animator;
private SpriteRenderer _spriteRenderer;
private int frameCount = 0;
private void Awake()
{
_playerControls = new PlayerControls();
// _animator = GetComponentInChildren<Animator>();
// _spriteRenderer = GetComponentInChildren<SpriteRenderer>();
_movePoint.parent = null;
_movePoint.position = transform.position;
}
private void OnEnable()
{
_playerControls.Player.Enable();
}
private void OnDisable()
{
_playerControls.Player.Disable();
}
private void Update()
{
MovePlayer();
}
private void MovePlayer()
{
frameCount++;
Debug.Log(frameCount +". I want to move from " + transform.position + " to " + _movePoint.position + " by " + _speed * Time.deltaTime + "each step.");
transform.position = Vector3.MoveTowards(transform.position, _movePoint.position, _speed * Time.deltaTime);
Debug.Log(frameCount + ". I want to move from " + transform.position + " to " + _movePoint.position + " by " + _speed * Time.deltaTime + "each step.");
if (Vector3.Distance(transform.position, _movePoint.position) <= .05f)
{
_playerMovement = _playerControls.Player.Move.ReadValue<Vector2>();
if (Mathf.Abs(_playerMovement.x) == 1f)
{
Vector3 checkPosition = _movePoint.position + new Vector3(_playerMovement.x, 0f, 0f);
Vector2 origin = transform.position;
RaycastHit2D hit = Physics2D.Raycast(origin, _playerMovement, 1f, _obstacles);
if (hit.collider == null)
{
_movePoint.position = checkPosition;
}
}
if (Mathf.Abs(_playerMovement.y) == 1f)
{
Vector3 checkPosition = _movePoint.position + new Vector3(0f, _playerMovement.y, 0f);
Vector2 origin = transform.position;
RaycastHit2D hit = Physics2D.Raycast(origin, _playerMovement, 1f, _obstacles);
if (hit.collider == null)
{
_movePoint.position = checkPosition;
}
}
}
}
}