- Home /
How do i script basic attack animation?
Hello, everyone, I'm currently having problems trying to add an animation to my attack script. The attack works and the dummy I have in unity loses damage and is destroyed when Health goes to zero but when I try to add the animation I made for the weapon I have it does not respond and I have no idea how to make it work I'm very new to scripting aspect of unity. And I'm very proud of what I've done so far, but I've been stuck on this problem for hours and do not know how to get around it any help would be greatly appreciated thank you.
I want to confirm these:
Are you sure your animation for weapon work fine? (I mean in editor simulator window or something like this)
Which animation system you are using: Unity legacy animation system or mecanim?
How your script look like and can you show us the structure of your character object on unity editor?
The info you gave us doesn't help much, we can not exactly help you solve the problem.
Hi sorry I thought I was being specific the animation was done in legacy and it does work but im having trouble making the melee script I have play it when i use the button to attack. And it is a first person so im only worried about the weapon animation.
I see, sr for missed understand, I though you add a new animation into your model, and this animation didn't work while others was fine. hmm, could you provide screenshot how you set your animation character?
Answer by Ginxx009 · Dec 28, 2017 at 03:53 AM
When you want to damage an AI with a Sword something like that you can basically apply this method to yours.
First put a collider
on the tip of your sword or on the whole sword then add a nametag to your AI something like AI tag and add this script to the sword .
OnTriggerEnter is called when the Collider other enters the trigger.
void OnTriggerEnter(Collider other)
{
//Your function for the health
healthminusminus();
}
void OnTriggerExit(Collider other)
{
//Do nothing
}
thank you for the help I'll let you know if this works
Hi, unfortunately, this didn't help me the current script I'm using has damage detection already.
could you provide a screenshot on how you did it on your inspector and the code structure .
Answer by chargecannons · Dec 28, 2017 at 06:26 AM
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class meleesys : MonoBehaviour { public int Damage = 50;
public float Distance;
public float maxDistance = 1.5f;
public Transform Maice;
RaycastHit hit;
void Update () {
if (Input.GetButtonDown("Fire1"))
{
Maice.animation.Play("attack");
if (Physics.Raycast(transform.position,
transform.TransformDirection(Vector3.forward), out hit)) {
Distance = hit.distance;
if(Distance < maxDistance)
hit.transform.SendMessage("ApplyDamage", Damage,
SendMessageOptions.DontRequireReceiver);
}
}
}
}
no it does damage but i cant get the animation i want to add to play when doing damage.
inside on your if statement try to add this line of code animation.Play("trustSword");
then tell me if it works
Answer by JVLVince · Dec 28, 2017 at 07:01 AM
well in case you use the legacy animation system, take a look here.
Animation m_anim;
void Start(){
m_anim = GetComponent<Animation>();
}
Then in you update function, add the code below. I think it's will work fine.
if (Physics.Raycast(transform.position,
transform.TransformDirection(Vector3.forward), out hit)) {
Distance = hit.distance;
if(Distance < maxDistance){
// Add this code:
m_anim.Play("<your weapon animation name>");
hit.transform.SendMessage("ApplyDamage", Damage,
SendMessageOptions.DontRequireReceiver);
}
}
yes, I just copy your code and edit to make sure you know where to put that code in :D
thank you, this helped because now it knows to look for animation but now it is telling me the that there is no animation on my "melee" game object when I want it to look for the animation on the "$$anonymous$$aice" object. I have "$$anonymous$$aice" child to "melee" and when I put the animation on the $$anonymous$$aice it disappears when I test it in game mode