Hello everyone. I already watched 3 videos and none of them worked for me. I have watched one of the videos and i wrote carefully everything what i saw.
It will be my 4’th attempt and finally decided to create topic about that but it gives me error
Assets\Script\ClickToMove.cs(21,26): error CS1955: Non-invocable member ‘InputAction.enabled’ cannot be used like a method.
I have tried to change input system old to new and still same error.
And here is the code. I cannot upload it as text file because it is not allowed for new members.
using System.Collections;
using UnityEngine;
using UnityEngine.InputSystem;
public class ClickToMove : MonoBehaviour
{
[SerializeField]
private InputAction mouseClickAction;
[SerializeField]
private float playerSpeed = 10f;
private Camera mainCamera;
private Coroutine coroutine;
private Vector3 targetPosition;
private void Awake() {
mainCamera = Camera.main;
}
private void OnEnable() {
mouseClickAction.enabled();
mouseClickAction.performed += Move;
}
private void OnDisable() {
mouseClickAction.performed -= Move;
mouseClickAction.Disable();
}
private void Move(InputAction.CallbackContext context) {
Ray ray = mainCamera.ScreenPointToRay(Mouse.current.position.ReadValue());
if (Physics.Raycast(ray: ray, hitInfo: out RaycastHit hit) && hit.collider) {
if (coroutine != null) StopCoroutine(coroutine);
coroutine = StartCoroutine(PlayerMoveTowards(hit.point));
targetPosition = hit.point;
}
}
private IEnumerator PlayerMoveTowards(Vector3 target) {
while (Vector3.Distance(transform.position, target) > 0.1f) {
Vector3 destination = Vector3.MoveTowards(transform.position, target, playerSpeed * Time.deltaTime);
transform.position = destination;
yield return null;
}
}
private void OnDrawGizmos() {
Gizmos.color = Color.red;
Gizmos.DrawSphere(targetPosition, 1);
}
}
