Getting a sword in place while attacking an enemy

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.

Ok I’ve fixed the issue!

Ticking the X Y X checkboxes for both Freeze Position and Freeze Rotation under the Constraints option fixed this. However, I have another issue. The sword being a solid object distorts my character’s movement - he stutters as he runs. The physics of the sword also makes my character bump up as he rolls on the ground.

Now I’ve noticed that this only happens when the sword’s Box Collider is enabled (which it needs to be based on the goals stated above), however I can’t see exactly where the problem lies. The sword doesn’t touch the floor, and my character doesn’t have a Box Collider or Rigidbody himself. He only has a Character Controller - could that be it?

Please help where you can, and thank you once again!

Add Fixed Joint to you character and set Connected body setting in joint’s properties to your sword. You can set Break force and Break torque to some values, if you want, for example, the sword fly out from the hand, if the strike is too strong.

Done it - assigning each GameObject to a layer and unticking them to cancel their collisions fixed the issue! :slight_smile: