Here is the code
public class PlayerController : MonoBehaviour
{
public float moveSpeed;
private bool isMoving;
private Vector2 input;
private Animator animator;
public LayerMask solidObjectsLayer;
public LayerMask interactableLayer;
private void Awake()
{
animator = GetComponent<Animator>();
}
private void Update()
{
if (!isMoving)
{
input.x = Input.GetAxisRaw("Horizontal");
input.y = Input.GetAxisRaw("Vertical");
if (input.x != 0) input.y = 0;
if (input != Vector2.zero)
{
animator.SetFloat("moveX", input.x);
animator.SetFloat("moveY", input.y);
Vector3 targetPos = transform.position;
targetPos.x += input.x * moveSpeed * Time.deltaTime;
targetPos.y += input.y * moveSpeed * Time.deltaTime;
if (IsWalkable(targetPos)) ;
StartCoroutine(Move(targetPos));
}
}
animator.SetBool("isMoving", isMoving);
if (Input.GetKeyDown(KeyCode.Z))
Interact();
}
void Interact()
{
var facingDir = new Vector3(animator.GetFloat("moveX"), animator.GetFloat("moveY"));
var interactPos = transform.position + facingDir;
//Debug.DrawLine(transform.position, interactPos, Color.red, 1f);
var collider = Physics2D.OverlapCircle(interactPos, 0.2f, interactableLayer);
if (collider != null)
{
collider.GetComponent<Interactable>()?.Interact();
}
IEnumerator Move(Vector3 targetPos)
{
isMoving = true;
while ((targetPos - transform.position).sqrMagnitude > Mathf.Epsilon)
{
transform.position = Vector3.MoveTowards(transform.position, targetPos, moveSpeed * Time.deltaTime);
yield return null;
}
transform.position = targetPos;
isMoving = false;
}
private bool IsWalkable(Vector3 targetPos)
{
// Check for collider overlaps within a circle centered at targetPos with a radius of 0.2f
// solidObjectsLayer and interactableLayer are assumed to be defined elsewhere in the class
if (Physics2D.OverlapCircle(targetPos, 0.2f, solidObjectsLayer | interactableLayer) != null)
{
// If an overlap is found, indicating that the position is not walkable, return false
return false;
}
// If no overlap is found, return true indicating that the position is walkable
return true;
}
}
}
The error is saying that private and public don’t go with the IsWalkable bool.
Is it possible that the above code is contained in some sort of local scope rather than at the top level of the class?
– dstearsOh ok ill add the whole doc code ok
– PonyoProthere you go, i think this is what you meant
– PonyoPro