- Home /
first person Melee combat with raycsasts in C#
Im making a first person stealth game like superhot but without the time slowing
How is the melee combat done in that
Answer by connorwforman · Oct 11, 2017 at 03:27 PM
Model and rig a character in blender. Name the weapon hand bone to "weaponHand" or something.
Make an animation for attacking.
Export as FBX into Unity, then make a sword separately.
Child the sword to
weaponHand
. Make a cube and child it to the sword. The cube's name should beSwordCollider
Disable the mesh renderer, and edit the collider to the sword.make a new C# script called meleeAttack
Make sure that you have the attack animation. Put it into an animation clip component and add that to the player.
This should be your script:
public float attackTimer = 2f; //However long you want the delay between attacks to be.
public Animation attack; //Drag the attack animation here
void Update () {
attackTimer -= Time.deltaTime;
if (attackTimer < 0)
{
if (Input.GetButtonDown("Fire1")) {
attack.Play();
}
}
}
This should be your enemyDamage script:
public float Sword_Damage = 33f; //Adjust damage here
public float enemyHealth = 100f;//Adjust health here
void OnTriggerEnter (Collider other) {
if (other.gameObject.name == SwordCollider) {
enemyHealth -= SwordDamge;
}
}
void Update () {
if (enemyHealth <= 0) {
destroy(gameObject);
}
}
You made a good point. @thedublinboy , use this as your first script:
public float attackTimer = 2f; //However long you want the delay between attacks to be.
public Animation attack; //Drag the attack animation here
public GameObject SwordCollider;
void Start () {
SwordCollider.SetActive(false);
}
void Update () {
attackTimer -= Time.deltaTime;
if (attackTimer < 0)
{
if (Input.GetButtonDown("Fire1")) {
SwordCollider.SetActive(true);
attack.Play();
SwordCollider.SetActive(false);
}
}
}
Your answer
![](https://koobas.hobune.stream/wayback/20220612140720im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Make melee attack in 2D game 1 Answer
Need help with animations & melee attack 1 Answer
Why i can't use method in gameObject.getComponent 1 Answer
How could I combine melee weapons and rays? 1 Answer
hold key unequip sword 0 Answers