Problem with simple attack button,,
Need some help,, i make attack button but doen't work, this my script :
using UnityEngine;
using System.Collections;
public class Attack : MonoBehaviour
{
private Animator animator;
// Use this for initialization
void Start ()
{
animator = GetComponent<Animator>();
}
// Update is called once per frame
void Update ()
{
animator.SetBool("Attack1", false);
}
public void Attack1 ()
{
animator.SetBool ("Attack1", true);
}
}
Answer by courageousmonkey · May 25, 2016 at 04:46 AM
Do you ever call Attack1()?
The code you're showing doesn't ever call Attack1() so you're never attacking.
Try something like this instead.
using UnityEngine;
using System.Collections;
public class Attack : MonoBehaviour
{
//intializing attack button to spacebar
//use different keycode if you want to attack with different button
private KeyCode attackButton = KeyCode.Space;
private Animator animator;
// Use this for initialization
void Start()
{
animator = GetComponent<Animator>();
}
// Update is called once per frame
void Update()
{
animator.SetBool("Attack1", false);
//if pressing down attack button
if (Input.GetKeyDown(attackButton))
{
Attack1();
}
}
public void Attack1()
{
animator.SetBool("Attack1", true);
}
}
This code will attack every time you press the spacebar.
Side Note: Consider using a trigger instead of a bool.
Triggers automatically set themselves to false before each Update.
In other words, you wouldn't need the line animator.SetBool("Attack1",false);
.
You would just have to replace animator.SetBool("Attack1",true)
with animator.SetTrigger("Attack1")
.
that work, but what i need is when i press ui button, not when i press keyboard button, that why i use public void for event trigger. Please help me T_T
Alrighty, I see what you're trying to do.
One way to do it is to use the button's OnClick method. You can do this by clicking on the Button object under Hierarchy in the editor. There should be a script component called Button already on it under Inspector. Look for OnClick, it should be toward the bottom.
Press the + button and choose the game object that has the Attack script and then find the Attack1() method. That should be it!
You should look through the Unity reference if my description is confusing. Here's a link: Unity Button
Your answer
Follow this Question
Related Questions
How to make more combo attack,, 1 Answer
Problem with combo attack -1 Answers
Problem with simple attack Attack ui Button 1 Answer
how can i make simple combo attack,, 0 Answers