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);
}
}
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").