- Home /
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.
Answer by AKJ9 · Aug 26, 2014 at 10:58 PM
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!
Answer by JustFun · Aug 26, 2014 at 09:35 PM
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.
Thanks @JustFun. I've added a FixedJoint to my character's hand (which automatically adds a Rigidbody to his hand), and dragged my sword into the ConnectedBody field. However, I still get the same problem. Please bear in $$anonymous$$d that the sword is already a child of the hand (parent). Is this a problem?
@JustFun I've even tried adding a FixedJoint to my whole character - but still no luck. The sword jerks his movement while running/rolling.
Answer by AKJ9 · Aug 27, 2014 at 11:57 AM
Done it - assigning each GameObject to a layer and unticking them to cancel their collisions fixed the issue! :-)
Your answer
Follow this Question
Related Questions
Why object goes sometimes through walls by adding force ? 2 Answers
How to detect which side of a box collider was hit? (Top, Bottom, Right, Left) 2D game/ C# 3 Answers
Stop sword from distorting character animation 3 Answers
Objects with colliders going through walls and each other. 3 Answers
Any way to ignore collision between rigidbodies and colliders/character controllers? 1 Answer