I am working through the project and I had an idea on setting up the movement to change based on the distance the user clicked the mouse. So for example if the player clicked or held down the left mouse button anywhere within 2.0f or 5.0f the character would only walk the distance. But if you clicked farther away then the player would run.
I have a idea how I want to set this up. But I am getting lost and overthinking.
``
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;
using UnityEngine.InputSystem;
public class Mover : MonoBehaviour
{
[SerializeField] Transform target;
Animator animator;
int forwardSpeedHash;
float currentMovement;
public float maximumWalkVelocity = 0.5f;
public float maximumRunVelocity = 2.0f;
public float maximumMouseWalkDistance = 2.0f;
public float maximumMouseRunDistance = 10.0f;
float distanceFromMouse;
bool mouseDistanceRun;
bool hasHit;
Ray ray;
RaycastHit hit;
// Start is called before the first frame update
void Start()
{
animator = GetComponent<Animator>();
forwardSpeedHash = Animator.StringToHash("forwardSpeed");
}
// Update is called once per frame
void Update()
{
// get key input from player
bool movementPressed = Input.GetMouseButton(0);
// get current maxMovement using turnary operator
float currentMaxVelocity = mouseDistanceRun ? maximumRunVelocity : maximumWalkVelocity;
if (movementPressed)
{
MoveToCursor();
}
UpdateAnimator();
}
// Move character where you click with left mouse button
private void MoveToCursor()
{
ray = Camera.main.ScreenPointToRay(Input.mousePosition);
hasHit = Physics.Raycast(ray, out hit);
if (hasHit)
{
GetComponent<NavMeshAgent>().destination = hit.point;
// distance from the player position and the position of the mouse click
distanceFromMouse = Vector3.Distance(transform.position, hit.point);
Debug.Log("The distance from the player positon: " + transform.position + " and the new destination: " + hit.point + " is: " + distanceFromMouse);
}
}
private void UpdateAnimator()
{
Vector3 velocity = GetComponent<NavMeshAgent>().velocity;
Vector3 localVelocity = transform.InverseTransformDirection(velocity);
float speed = localVelocity.z;
GetComponent<Animator>().SetFloat(forwardSpeedHash, speed);
}
}
``
I can add an image of the character animator controller as well if needed.