- Home /
When the mouse is moving and the player is walking why the player is stuttering ?
I'm using the tmpMousePosition variable to check if the mouse is moving or not. If the mouse is moving make the part of the code and also go into "Walk" mode.
Then else if the mouse is not moving go into "Idle" mode.
The problem is when the mouse is moving the player is walking but also stuttering each second or so. Before using the variable tmpMousePosition and before doing the part in the Update function using the tmpMousePosition he was walking fine. Only now when adding the tmpMousePosition part it's stuttering like walking but not smooth like walking but something interrupt it.
My main goal to do is when moving mouse be in Walk mode when not moving the mouse be in Idle mode. The problem is the sttutering.
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class WorldInteraction : MonoBehaviour
{
public int speed = 5; // Determines how quickly object moves towards position
public float rotationSpeed = 5f;
private Animator _animator;
private bool toMove = true;
private Vector3 tmpMousePosition;
UnityEngine.AI.NavMeshAgent playerAgent;
private void Start()
{
tmpMousePosition = Input.mousePosition;
_animator = GetComponent<Animator>();
_animator.CrossFade("Idle", 0);
playerAgent = GetComponent<UnityEngine.AI.NavMeshAgent>();
}
private void Update()
{
if (Input.GetMouseButtonDown(0) && toMove == false &&
!UnityEngine.EventSystems.EventSystem.current.IsPointerOverGameObject())
{
toMove = true;
}
if (Input.GetMouseButtonDown(1) && toMove == true)
{
toMove = false;
}
if (toMove == true)
{
GetInteraction();
}
}
void GetInteraction()
{
if (tmpMousePosition != Input.mousePosition)
{
tmpMousePosition = Input.mousePosition;
_animator.CrossFade("Walk", 0);
Ray interactionRay = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit interactionInfo;
if (Physics.Raycast(interactionRay, out interactionInfo, Mathf.Infinity))
{
GameObject interactedObject = interactionInfo.collider.gameObject;
if (interactedObject.tag == "Interactable Object")
{
interactedObject.GetComponent<Interactable>().MoveToInteraction(playerAgent);
}
else
{
playerAgent.destination = interactionInfo.point;
}
}
}
else
{
_animator.CrossFade("Idle", 0);
}
}
}
Hi, only a quick guess, maybe there are times where the Input.mousePosition is the same in the previous and the current view (maybe when the mouse is moved slowly)?. This would trigger the _animator.CrossFade("Idle", 0);
in your line 61. Also, you may want to check if your character is already in a certain animation state before switching to it. For example checking if the player is already in the "Walk" animation before setting _animator.CrossFade("Walk", 0);
Your answer
Follow this Question
Related Questions
Why when creating new animator controller for the character the character is not walking right ? 0 Answers
How can i Instantiate on the terrain from left to right ? 0 Answers
Why the tile map scripts take almost all the cpu usage ? cpu usage is getting to 99% at times 1 Answer
How can i rotate object by pressing on key R and keep object facing to me my self ? 0 Answers
How can i spawn new gameobjects to be inside the terrain area ? 2 Answers