Change speed of animation by tapping keyboard
Hi I have written some code that lets me change the animation and navmeshagent speed while pressing any key on the keyboard. I am trying to make it so that it only adds to the speed when the keyboard is taped consecutively but right now it only works when a key is held down. Can someone please advise?
Here is the code I have: using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.AI;
public class AnimationSpeed : MonoBehaviour {
//private Animation anim;
public float Speed = 1;
public float NavSpeed = 0;
//private NavMeshAgent ourenemy;
Animator m_Animator;
//Value from the slider, and it converts to speed level
//float m_MySliderValue;
bool m_runFaster;
bool m_run;
void Start()
{
//Get the animator, attached to the GameObject you are intending to animate.
m_Animator = gameObject.GetComponent<Animator>();
NavSpeed = GetComponent<NavMeshAgent> ().speed;
}
void Update()
{
if (m_runFaster == false)
m_Animator.SetBool("runFaster", false); // animation condition bool
//The GameObject is jumping, so send the Boolean as enabled to the Animator. The jump animation plays.
if (m_runFaster == true)
m_Animator.SetBool("runFaster", true);
if (m_run == false)
m_Animator.SetBool("run", false);
//The GameObject is jumping, so send the Boolean as enabled to the Animator. The jump animation plays.
if (m_run == true)
m_Animator.SetBool("run", true);
if (Input.anyKey) // if tap adds .01 to speed total
{
Speed = Speed + .01f;
m_Animator.speed = Speed;
m_runFaster = true;
m_run = false;
GetComponent<NavMeshAgent> ().speed = 5 + Speed;
Debug.Log("A key or mouse click has been detected");
}
if (Speed > 2) // speed limit
{
Speed = 1;
GetComponent<NavMeshAgent> ().speed = 5 + Speed;
}
if( !Input.anyKey ){
m_run = true;
m_runFaster = false;
m_Animator.speed = 1;
Speed = 1;
GetComponent<NavMeshAgent> ().speed = 4 + Speed; // speed if no tap nav speed is 4 Speed is animation speed
}
}
}
Your answer
Follow this Question
Related Questions
Change speed of animation by tapping keyboard ,how to add to speed by tapping "anykey" consecutively 0 Answers
Creating prefab through Zenject Factory makes navMeshAgent act strange 0 Answers
how can I implement navmesh in this script? 0 Answers
NavMeshAgent dont find the right way on runtime build NavMesh 0 Answers
Navmesh Agent - The Y Next Position & Y Velocity does not update if there is no X velocity. 0 Answers