collision scripting

hello, im rather newish to unity i work in torque mostly, basically i used a bastardised version of the fpsplayer script to set me health to 100, attached to the player ofc. Now i have a model that is to repersent an enemy, and on collision it applys damage then destroys it self.

function OnCollisionEnter( collision : FPSPlayer )
{

FPSPlayer.hitPoints-=100;
Destroy(this);
}

How do i refrence the hitpoints as this script is attached to the enemy. This has an error asking for an instance of FPS so i tried

FPSplayer = new player
player.hitPoints -= 100;

but that didnt work either and would create a new player

any ideas, never worked in js before

function OnCollisionEnter (collisionInfo : Collision) : void

When OnCollisionEnter is called, it is automatically assigned a Collision variable. You must not change that.

So, you get a Collision variable, which contains informations…

Like gameObject, collider, rigidbody (to refer to the gameObject itself, or the Collider component of the gameObject, or the Rigidbody component of the gameObject.

So it would be :

function OnCollisionEnter (collisionInfo : Collision) {
	collisionInfo.gameObject.GetComponent(FPSPlayer).hitPoints -= 100;
	Destroy (gameObject);
};

Note : I’m not sure “this” works in Unity…Flash and ActionScript, sure, but Unity…? I don’t think so.

Edit : But I don’t also think you can access a custom component like this. You have to search for it before, with a GetComponent.

Edit2 : forgot a semi colon.

i just tried to run that, it come up with a semi colon error line 3 :S I dont know much about JS but that looks fine to me

Well, as the error say, I forget a semi colon ; at the specified line.

hmm doesnt seem to be working no collision is made, i have a mesh collider on both objects any suggestions

function OnCollisionEnter (collisionInfo : Collision)
{

var player : FPSPlayer = collisionInfo.gameObject.GetComponent(FPSPlayer);


	if (collisionInfo.gameObject.tag == "evil")
	{
	player.ApplyDamage(10000);
	Destroy (gameObject); 
	}
}

this is where i got up to and still no collision is happening and i cant work out why

btw the “evil” part is the tag of the object

Is the function called at least ? Put a Debug.Log (“function is called”); in the OnCollisionEnter.

I am reading this, and at the middle of the page, there is a pretty disturbing table…

I guess one of the objects must have a rigidbody…

yes and no, very occasionally it will fire, atm its not tho, i did have a refrence error witht he damage however when it did fire and was attached to the player insted it would delete everything on screen

And like this ?

function OnCollisionEnter (collisionInfo : Collision)
{
	var hisFPSPlayer : FPSPlayer;
	hisFPSPlayer = collisionInfo.gameObject.GetComponent(FPSPlayer);

	if (collisionInfo.gameObject.tag == "evil")
	{
		hisFPSPlayer.ApplyDamage(10000);
		Destroy (gameObject);
	}
}

right now were getting somewhere its firing however it has a nullrefrence error for this line

hisFPSPlayer.ApplyDamage(10000);

Try this :

function OnCollisionEnter (collisionInfo : Collision)
{
	var hisFPSPlayer : FPSPlayer;
	hisFPSPlayer = collisionInfo.gameObject.GetComponent(FPSPlayer);
	if (hisFPSPlayer)
		Debug.Log ("hisFPSPlayer component found");
	if (collisionInfo.gameObject.tag == "evil")
	{
		Debug.Log (" gameObject with tag evil found");
		hisFPSPlayer.ApplyDamage(10000);
		Destroy (gameObject);
	}
}

Do you have the messages in the console ?

it fires then comes up witha refrence error would that suggest it could not find applied damage in the FPSPlayer script?

This is FPSPlayer which i’ve changed a little to give u some idea

var maximumHitPoints = 100.0;
var hitPoints = 100.0;



var walkSounds : AudioClip;
var painLittle : AudioClip;
var painBig : AudioClip;
var die : AudioClip;
var audioStepLength = 0.3;


private var gotHitTimer = -1.0;

var rocketTextures : Texture[];



function ApplyDamage (damage : float) {
	if (hitPoints < 0.0)
		return;

	// Apply damage
	hitPoints -= damage;

	// Play pain sound when getting hit - but don't play so often
	if (Time.time > gotHitTimer  painBig  painLittle) {
		// Play a big pain sound
		if (hitPoints < maximumHitPoints * 0.2 || damage > 20) {
			audio.PlayOneShot(painBig, 1.0 / audio.volume);
			gotHitTimer = Time.time + Random.Range(painBig.length * 2, painBig.length * 3);
		} else {
			// Play a small pain sound
			audio.PlayOneShot(painLittle, 1.0 / audio.volume);
			gotHitTimer = Time.time + Random.Range(painLittle.length * 2, painLittle.length * 3);
		}
	}

	// Are we dead?
	if (hitPoints < 0.0)
		Die();
}

function Die () {
	if (die)
		AudioSource.PlayClipAtPoint(die, transform.position);
	
	// Disable all script behaviours (Essentially deactivating player control)
	var coms : Component[] = GetComponentsInChildren(MonoBehaviour);
	for (var b in coms) {
		var p : MonoBehaviour = b as MonoBehaviour;
		if (p)
			p.enabled = false;
	}

	LevelLoadFade.FadeAndLoadLevel(Application.loadedLevel, Color.white, 2.0);
}



function PlayStepSounds () {
	var controller : CharacterController = GetComponent(CharacterController);

	while (true) {
		if (controller.isGrounded  controller.velocity.magnitude > 0.3) {
			audio.PlayOneShot(walkSounds, 1.0 / audio.volume);
	
			yield WaitForSeconds(audioStepLength);
		} else {
			yield;
		}
	}
}

Just tried calling the function die() using

hisFPSPlayer.Die();

same error, it cant access FPSPlayer

The path must be incorrect then. Show us again the part where you call ApplyDamage please (unless it is the same script as we wrote ?).

function ApplyDamage (damage : float) {
   if (hitPoints < 0.0)
      return;

   // Apply damage
   hitPoints -= damage;

   // Play pain sound when getting hit - but don't play so often
   if (Time.time > gotHitTimer  painBig  painLittle) {
      // Play a big pain sound
      if (hitPoints < maximumHitPoints * 0.2 || damage > 20) {
         audio.PlayOneShot(painBig, 1.0 / audio.volume);
         gotHitTimer = Time.time + Random.Range(painBig.length * 2, painBig.length * 3);
      } else {
         // Play a small pain sound
         audio.PlayOneShot(painLittle, 1.0 / audio.volume);
         gotHitTimer = Time.time + Random.Range(painLittle.length * 2, painLittle.length * 3);

from FPSPlayer

What are the objects ? Are they children of an other object ? Where are the scripts ? Screenshot your hierarchy and show us it please.

Uploaded with ImageShack.us

2 actors :

  • First Person Controller : has FPSPlayer, and detection collision script,
  • Objects with tag “evil”.

When First Person Controller touches an object with tag evil, it triggers the detection collision script, and call Apply Damage to First Person Controller.

Am I right ?