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…
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);
}
}
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:
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