Different animations based in mouse click distance

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.

I reverted the code back to this level so I can work up from here with integrating this different idea.

Hi,

You could look into some scripts that peope mde for a strategy game camera,
which moves the camera at diffrent speeds, depending how close the pointer is to the edge,
some system like that could probably work for walking/running based on how far the pointer is
from center screen maybe.

Instead of raycasting you could look into this to get the mouse pos in the “world” ;

I believe there are also ways to calculate the navmesh path distance,
you could let the character walk or run based on the distance.

Thats some ideas anyways, gl with your game!

Your Animator should already be set up to walk at a certain speed and run at another (and blend between the two).

The NavMeshAgent can be given a speed. All you need to do is check the distance remaining.

        void UpdateMovementSpeed()
        {
            navMeshAgent.speed = navMeshAgent.remainingDistance < 5 ? walkingSpeed: maxSpeed;
        }

You’ll need to set a walking speed for this.

Note that this will conflict with a later lesson when we have patrolling happening. Our enemy logic is to run to combat, and walk to patrol points. A good solution for this is in Update() something like this:

void Update()
{
     navMeshAgent.enabled = !health.IsDead(); //This seriously needs refactoring
     if(gameObject.CompareTag("Player") UpdateMovementSpeed();
     UpdateAnimator();
}

I will take a look and give it a try.

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.