Sword Does Damage To Player...

Hey, so I have this small issue and I cant seem to fix it, the sword that the player is holding works and applies damage, but it also hits the player, Ive tried using ignore collision but im not sure whats wrong.

Im trying to ignore the character Controller collision in order for this to work, but im getting the worst of luck, heres the sword script:

var Sound : AudioClip;
var force = 10.0;
var damage = 200; 

var dagger : Collider;

function Start () {

    var dagger = Instantiate (dagger);
	Physics.IgnoreCollision(dagger.collider, transform.root.collider);
}

function Fire1 () { 

var direction = transform.TransformDirection(Vector3.forward); 
var hit : RaycastHit; 

// Did we hit anything? 
if (Physics.Raycast (transform.position, direction, hit)) { 

// Apply a force to the rigidbody we hit 
      if (hit.rigidbody) 
	  hit.rigidbody.AddForceAtPosition(force * direction, hit.point); 

hit.collider.SendMessageUpwards("ApplyDamage", damage, SendMessageOptions.DontRequireReceiver);

	}
}

Also, I seem to be able to hit the enemy 10 meters away from me with a sword… :confused:

Any ideas?

Not sure why it hits you but to put a limit on the raycast change:

if (Physics.Raycast (transform.position, direction, hit))

To this:

if (Physics.Raycast (transform.position, direction, hit, distance))

Of corse you will have to add a “distance” var to set how far you want it to go.

Thanks king, I got one problem fix, now im just trying to figure out why the sword hits me aswell…

greetings :slight_smile: if your player has a tag you could check the tag name to make sure its not your player that was hit (untested):

if (Physics.Raycast (transform.position, direction, hit)  hit.rigidbody.gameObject.tag != "my_player") {

Hey gman, I understand your idea, but how do i give the player a tag, and what am i suppose to write in “My_player”

var Sound : AudioClip;
var force = 10.0;
var damage = 200; 
var distance = 2;

//var dagger : Collider;
//function Update () {
 //  var dagger = Instantiate (dagger);
 //   Physics.IgnoreCollision(dagger.collider,transform.root.collider);
//}

function Fire1 () { 

var direction = transform.TransformDirection(Vector3.forward); 
var hit : RaycastHit; 

// Did we hit anything? 
if (Physics.Raycast (transform.position, direction, hit, distance)  hit.rigidbody.gameObject.tag != "FirstPersonPlayer") {

hit.collider.SendMessageUpwards("ApplyDamage", damage, SendMessageOptions.DontRequireReceiver);
	}
}

I get this error:

NullReferenceException
Dagger.Fire1 () (at Assets\WeaponScripts\Dagger.js:21)
UnityEngine.Component:BroadcastMessage(String, Object, SendMessageOptions)
UnityEngine.Component:BroadcastMessage(String, Object, SendMessageOptions)
UnityEngine.Component:BroadcastMessage(String)
PlayerWeapons:Update() (at Assets\WeaponScripts\PlayerWeapons.js:16)

in the inspector for your player object at the top is a tag dropdown. right now it probably says “untagged”. click the dropdown and select “add tag”. you will see a list of “Element” items started with 0. for the next available one, type in a tag name for your player object and press enter. now click your player object again in the heirarchy. this time when you click the dropdown it will have the option you added and you can select it. this is the text you can check against.

after you get the tag setup, you may need to change hit.rigidbody to hit.collider in my example:

hit.collider.gameObject.tag != "FirstPersonPlayer"

Hey it worked! But for some reason, the sword only applys damage when you hold it high and aim for the enemies head, the sword collider fits the sword and is on the same gameobject as the sword script is in, how come it does that?

edit: I figured it out, i just put the sword script/collider/ridigbody to another child, thanks for all the help guys :slight_smile:

What where the scripts needed?

i relly need some help with a third person shooter type thing where you can use a sword! please help me i need an example project :slight_smile: