- Home /
Key Press to Trigger animation when within range
Im trying to make a script that can play an Animation. Not like normal, but a GUI pops up once close to the Object (Saying "[k]" or something). And once you press the key, the animation starts playing, but only if within range of the Object. Im a little noob to this.. so i would like some help :)
Answer by Sasstraliss · Sep 12, 2013 at 06:11 AM
Look up how to start animations.
As for the input, you've got two options. Either hardcode the keystroke in, or use the Input Manager.
if (Input.GetKeyDown('k'))
{
//Begin animation
}
It's up to you how you interface with Animations. I myself use the Legacy system.
public class AnimationTest : MonoBehaviour
{
private Animation _anim;
private bool closeEnough = false;
void Start()
{
_anim = this.GetComponent<Animation>();
}
void Update()
{
if (Input.GetKeyDown("k") && closeEnough == true)
{
_anim.Play("ANIMATIONNAMEHERE");
}
}
void OnGUI()
{
if (Vector3.Distance(targetObject.transform.position, player.transform.position) < desiredDistance)
{
GUI.Label(new Rect(10, 10, 200, 20), "[k]");
closeEnough = true;
}
}
}
You'll want to look up how to deal with animations in code. I myself am probably using an outdated/non normal method to do it. The logic I've provided should work well enough I'd hope.
Your answer
Follow this Question
Related Questions
Pointers to Show Off-Screen Enemies 1 Answer
Make a turret shoot multiple targets 1 Answer
OnTriggerEnter Glitch 1 Answer
Tagging object while instating not working. 3 Answers
Very simple inventory script... 0 Answers