The question is answered, right answer was accepted
Attack Script
Hi, I'm new in Unity and I'd like to create a simple game: a main character with a couple of attacks (with a sword) to kill aliens. For now, she stands in the "Idle" position and she "walks" but I've been searching and following some tutorials and trying out some scripts for the attacks but none of them seems to work. Anyone could help me or send me a basic script to do the attacks, the animations of the attacks are already done, they're called "AttackNear" and "AttackMiddle" and I'd like them to be activated with mouse left and right button for example. PLEASE I NEED HELP!! Also, this is how I've set the Animator:
Answer by MichaelNielsenDev · Jul 14, 2016 at 11:20 PM
No one is going to write your scripts for you, but I can give you some tips and resources to start.
If you want to one thing to attack another, typically you want the attacker to have a method on one of their scripts that reduces the health of the thing they are attacking. Health can be a public variable for any classes that can be attacked.
Accessing what you are hitting can be done a few different ways. My favorite and the most simple way is to assign tags to gameobjects. You'd tag enemies with the Enemy tag, and your player with the Player tag.
Then, you'd either use a raycast or OnCollisionEnter to get data on the thing you hit, and it's there that you check what tag that thing has. If it has the Enemy tag, you reduce their health by a certain amount.
Also, there's OnTriggerEnter, which functions like OnCollisionEnter but with trigger colliders. With these, make sure you mark the collider as a trigger, which you can do by going into the collider component of the gameobject you're interested in and check Is Trigger.
More info on Tags, Raycasts, OnCollsionEnter, and OnTriggerEnter: https://docs.unity3d.com/Manual/Tags.html https://docs.unity3d.com/ScriptReference/Physics.Raycast.html https://docs.unity3d.com/ScriptReference/Collider.OnCollisionEnter.html https://docs.unity3d.com/ScriptReference/Collider.OnTriggerEnter.html
If all else fails, there are plenty of tutorials on youtube showing how to attack and damage things.
Here's one I recommend: https://www.youtube.com/watch?v=qwuPiaFU37w
Answer by theANMATOR2b · Jul 14, 2016 at 11:18 PM
Maybe a better option is you look at the learn section.
Specifically the animation learn section. Also in the live training section where several instances the trainer writes animation code.
Its better to learn how to do something instead of having someone do it for you.