Hi all,
I am a begginer at Unity so sorry if this seems like a repeat of other questions or if it's unclear but I have been staring at my code for about an hour now and looking all over the place for answers, this will be my last resort.
So...
I have created the start of an FPS as a test following some basic tutorials online. This is my Javascript raycast script I have attached to an empty and grouped to the gun in the hierarchy.
var DamageAmount : int = 5;
var TargetDistance : float;
var AllowedRange : float = 15;
function Update () {
if(Input.GetButtonDown("Fire1")) {
var Shot : RaycastHit;
if (Physics.Raycast (transform.position, transform.TransformDirection(Vector3.forward), Shot)) {
TargetDistance = Shot.distance;
if (TargetDistance < AllowedRange) {
Shot.transform.SendMessage("DeductPoints", DamageAmount, SendMessageOptions.DontRequireReciever);
}
}
}
}
![alt text][1]
(The empty is named "GunMechanics")
This is the enemy's script:
var EnemyHealth : int = 10;
function DeductPoints (DamageAmount : int) {
EnemyHealth -= DamageAmount;
}
function Update () {
if (EnemyHealth <= 0) {
Destroy(gameObject);
}
}
and this is the the gun firing script:
function Update () {
if(Input.GetButtonDown("Fire1")) {
var gunsound : AudioSource = GetComponent.();
gunsound.Play();
GetComponent.().Play("GunShoot");
}
}
These are all the scripts in the game so far.
Now this all works fine except the enemy's health doesn't go down at all. The empty is rotated correctly and is positioned at the barrel of the gun.
I know the raycast collides with objects because the target distance changes everytime I fire.
I'm unsure if I have two problems here or if they are connected but as well as the enemy not taking any damage, the console throws this error:
![alt text][2]
This has completely dumbstruck me.
Again I would be very appreciative to anyone that helps me, it's a constant learning process (starting last week)
Apologies once more.
[1]: /storage/temp/99752-help2.png
[2]: /storage/temp/99753-help4.png
↧