Knifing someone help

Ok i need some help i have my script which will be attached to the knife of the player. So when my player hits the r button he will knife his opponent. This will be done by spawning the knife animated knife while moving so player wont get hurt as well. Here is my script for actually hurting my enemy.

var damage = 500;

function OnTriggerEnter (collision : hit)
{
    var hit : Raycast;
    if ( collision : hit )
    {
        hit.collider.SendMessageUpwards("ApplyDamage", damage, SendMessageOptions.DontRequireReciever);
    }
}

So what it does is it attacks the foe and kills them instantly.

Some points:

  • OnTriggerEnter have nothing to do with Raycasts
  • There is no type “hit” (even if collision should be the type it would be wrong since it’s Collision with capital C)
  • OnTriggerEnter does not take a Collision it takes the Collider that enters the trigger.
  • Your if statement… well, is completely wrong. The expression within the brackets should result in a boolean value (true or false). A colon has no use in the expression since it’s used to specify the type when declaring variables…

I think you want something like that:

var damage = 500;

function OnTriggerEnter ( other : Collider )
{
    if ( other.tag == "Player" ) // or what ever you want to identify the object
    {
        other.SendMessageUpwards("ApplyDamage", damage, SendMessageOptions.DontRequireReciever);
    }
}

this is what i tried but i still have a litle problem, if you double click really fast it hits twice!!!

var range = 10.0;
var fireRate = 1.0;
var force = 100.0;
var damage = 100.0;
var maxHits = false;
var Maxhits = 80;
var delay = 0.5;
var Sound : AudioClip;
var HitSound : AudioClip;
var DirectionPoint : Transform;
var AnimationModel : Transform;
var Weapons : Transform;
var Sniper : Transform;
var MachineGun : Transform;
private var hitParticles : ParticleEmitter;
var knifing = false;
var KnifingM = false;

var HitsLeft : int = 0;
private var nextFireTime = 0.0;
private var m_LastFrameShot = -1;

function Start () {
	hitParticles = GetComponentInChildren(ParticleEmitter);
	
	// We don't want to emit particles all the time, only when we hit something.
	if (hitParticles)
		hitParticles.emit = false;
	HitsLeft = Maxhits;
}
function Update(){;

if(Input.GetButtonDown("Melee") && (PlayerWeapons.sniper == true) && knifing == false){
Sniper.animation.Play("putawaysniper");
PlayerWeapons.weapon = false;
FireWithS();

}

if(Input.GetButtonDown("Melee") && (PlayerWeapons.machineGun == true) && knifing == false){
knifing = true;
KnifingM = true;
MachineGun.animation.Play("PutAwayMachineGun");
PlayerWeapons.weapon = false;



}
if(KnifingM ==true)
FireWithM();

}


function FireWithS () {

if(knifing == true){
yield WaitForSeconds(delay);
	if (HitsLeft == 0)
		return;
	
	// If there is more than one bullet between the last and this frame
	// Reset the nextFireTime
	if (Time.time - fireRate > nextFireTime)
		nextFireTime = Time.time - Time.deltaTime;
	
	// Keep firing until we used up the fire time
	while( nextFireTime < Time.time && HitsLeft != 0) {
		
		FireOneShot();
		nextFireTime += fireRate;
	
	AnimationModel.animation.Play();
		if(Sound)
	audio.PlayOneShot(Sound);
	yield WaitForSeconds(delay);
	Sniper.animation.Play("TakeOutSniper");
	yield WaitForSeconds(delay);
	PlayerWeapons.weapon = true;
}
	
	
}
}


function FireWithM () {
knifing = true;
if(knifing == true){
yield WaitForSeconds(delay);
	if (HitsLeft == 0)
		return;
	
	// If there is more than one bullet between the last and this frame
	// Reset the nextFireTime
	if (Time.time - fireRate > nextFireTime)
		nextFireTime = Time.time - Time.deltaTime;
	
	// Keep firing until we used up the fire time
	while( nextFireTime < Time.time && HitsLeft != 0) {
		
		FireOneShot();
		nextFireTime += fireRate;
		
	AnimationModel.animation.Play();
		if(Sound)
	audio.PlayOneShot(Sound);
	yield WaitForSeconds(delay);
	MachineGun.animation.Play("TakeOutMachineGun");
	
	KnifingM = false;
	yield WaitForSeconds(delay);
	PlayerWeapons.weapon = true;
	yield WaitForSeconds(0.01);
	knifing = false;
}
	
	
}
}

function FireOneShot () {
if(knifing ==true){
	var direction = DirectionPoint.transform.forward;
	var hit : RaycastHit;
	
	// Did we hit anything?
	if (Physics.Raycast (DirectionPoint.transform.position, direction, hit, range)) {
		// Apply a force to the rigidbody we hit
		if (hit.rigidbody)
			hit.rigidbody.AddForceAtPosition(force * direction, hit.point);
		
		// Place the particle system for spawing out of place where we hit the surface!
		// And spawn a couple of particles
		if (hitParticles) {
			hitParticles.transform.position = hit.point;
			hitParticles.transform.rotation = Quaternion.FromToRotation(Vector3.up, hit.normal);
			hitParticles.Emit();
			if(HitSound)
			audio.PlayOneShot(HitSound);
		}

		// Send a damage message to the hit object			
		hit.collider.SendMessageUpwards("ApplyDamage", damage, SendMessageOptions.DontRequireReceiver);
	}
	if(Maxhits == true)
	HitsLeft--;

	// Register that we shot this frame,
	// so that the LateUpdate function enabled the muzzleflash renderer for one frame
	m_LastFrameShot = Time.frameCount;
	enabled = true;
	knifing = false;
		
}
}