- Home /
solved
Animation Input / ETC
Hello i would like to ask about issue that i got what should i do im still newbie on this thing...
How to make my Run animation can be Hold Key like (Left Shift) or ETC i already tried with "Input.GetKey" but it did't work so i make it to "Input.GetKeyDown" so the input actually like a Left Shift Toggle.Question 1
(Look at the Picture) ![alt text][1] it better config like the animation form the aniamtion tab or the Script it self? a lot i see in the Youtube some Youtuber use those method because they say "more easy" it that true??Question 2
Yup that all That i want to ask and here the
CODE
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Controller : MonoBehaviour
{
public float Speed = 10f;
public Animator anim;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
PlayerMovement();
InputCheck();
}
void InputCheck()
{
if (Input.GetKeyDown(KeyCode.W))
print("W Pressed!");
if (Input.GetKeyDown(KeyCode.S))
print ("S Presed!");
if (Input.GetKeyDown(KeyCode.A))
print("A Pressed!");
if (Input.GetKeyDown(KeyCode.D))
print("D Presed!");
}
void PlayerMovement()
{
anim.SetFloat ("Vertical", Input.GetAxis("Vertical"));
anim.SetFloat ("Horizontal", Input.GetAxis("Horizontal"));
if (Input.GetKeyDown(KeyCode.LeftShift))
{
print("Shift Pressed!");
anim.SetTrigger("Run");
}
if (Input.GetKeyDown(KeyCode.Space))
{
print("Space Pressed!");
anim.SetTrigger("Jump");
}
if (Input.GetKeyDown(KeyCode.C))
{
print("C Pressed!");
anim.SetTrigger("Crouch");
}
}
}
Yeah im hoping that i can become Game Devoloper/Game Design/ETC some day's actully im learning on my own
(im bit lazy to read the Documention because to long lol ) so i warp my head to think what code should do if you guys can help me a lot!
Answer by DevManuel · Feb 17, 2021 at 07:36 AM
You could do something like this:
public class Controller : MonoBehaviour
{
public float Speed = 10f;
public Animator anim;
// Update is called once per frame
void Update()
{
PlayerMovement();
}
void PlayerMovement()
{
anim.SetFloat ("Vertical", Input.GetAxis("Vertical"));
anim.SetFloat ("Horizontal", Input.GetAxis("Horizontal"));
if (Input.GetKeyDown(KeyCode.LeftShift))
{
print("Shift Pressed!");
//anim.SetTrigger("Run");
anim.SetBool("Run", true);
}else{
anim.SetBool("Run", false);
}
// ...
}
}
I hope it helps.
Thanks for the Respond! The Code is working fine whean i try to change the paramater to Bool it does't work whean i try to hold and unHold the Left Shift it keep on what shoud i do?
and i also make a change for some animation plaese look at the Screen Shot
Have you changed the conditions in the animation controller to? I mean, now you have switched form trigger to bool, but is it already updated at the conditions? For me it's a little bit confused to see these arrows. Do you ave an arrow withe the right condition from idle to run and BACK to idle? Maybe you just remove the other states to only test run.
So Your state change on start to walk without any condition and then if you press left shift the run state is activated but if you release it don't turn back to walk. Did I understood it correctly?
The next step would be debugging if the "bug" occurs in the script or in the animation controller. So my question: Please add a debug log to your code to test if the script works correctly. Then, changes the bool in the animation controller correctly?
Yes that what i want "whean i try to walk, and hold Left Shift it will run the "Run State" and when i unhold it will back to walking state. because my walk condition i use the Vertical Greater/Less 0.1
Follow this Question
Related Questions
How to play animation once in Input.GetMouseButton 1 Answer
Distribute terrain in zones 3 Answers
Unity C# Two keys pressing one after another not doing a function vice versa 0 Answers
Multiple Cars not working 1 Answer
How to add Animation with C# 0 Answers