I’m using scripts for my character’s sword that I previously used for another project of mine, except it was for a fireball. I want the sword to damage an enemy, and stay in my character’s hand. The damage works, although the sword flies off around the scene should it come in contact with the floor or an enemy.
If I turn the Is Kinematic Rigidbody setting on for my sword, the sword stays in place except the enemy does not react to being hit by the sword. So I can achieve one of the two goals depending on the Is Kinematic option. Please review my script and let me know what I am doing wrong for this to happen. The only relevant script that could be part of the problem is this script that’s placed on the enemy to detect the sword:
using UnityEngine;
using System.Collections;
public class BossScript : MonoBehaviour {
public static bool dead;
public AudioClip hitSound;
void Awake()
{
print ("attached to: "+ name);
}
public int bossHealth = 3;
void OnCollisionEnter (Collision collision){
if(collision.gameObject.tag == "Sword" && CharacterAnimationController.attacking==true){
animation.Play("flinch");
audio.PlayOneShot(hitSound);
bossHealth--;
Debug.Log("reduced health to: " + bossHealth);
if(bossHealth<=0){
dead = true;
animation.Play("dead");
} } }
Also bear in mind the following properties:
Sword:
Box Collider (Is Trigger unticked),
Rigidbody (Use Gravity and Is Kinematic unticked)
Enemy:
Box Collider (Is Trigger unticked),
Rigidbody (Use Gravity and Is Kinematic ticked)
Do I need both a Box Collider and a Rigidbody on both objects? As mentioned, I am using the script I used for my previous project’s fireball, and that worked perfectly (although the fireball moved when fired, whereas the sword should stay put in my character’s hand).
Many thanks for your time and help in advance.