- Home /
What am I doing wrong with this attack script?
Hello,
I am a complete and total newbie to scripting and Unity in general. I am trying to create a simple attack button by pressing the right ctrl button. I finally managed to learn how after a week of research, but the animation will only activate if I continue to press the button rapidly, and it plays rather slow.
How can I get my animation to play on its own when I simply tap the button once? and more quickly? And on a side question whats a basic script for sound effects when attacking?
This is the script I am using:
public class PlayerAttack : MonoBehaviour { public GameObject target;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
if(Input.GetKeyUp(KeyCode.RightControl))
animation.Play("Gun Attack 2");
}
}
Answer by xt-xylophone · Mar 19, 2013 at 09:04 AM
Getkeyinputup happens when you release the key, try getkeyinputdown for when you press it
For the sound, you could declare a public variable above your start method like:
public AudioSource attackSound;
Then In the editor drag your sound clip into that variable in the inspector.
Then in the code where you want to play it you can use
attackSound.Play();
Hope that helps!
Answer by Member · Mar 28, 2013 at 05:51 PM
I finally figured out how to get the animation how to work, and I had to get creative with sound effects by learning how to create a projectile prefab, and have the sound play on the prefabs creation. Thanks anyway