Hi, I’ve been taking a series of YouTube tutorials for building a basic game: in one of the tutorials I was shown how to attack enemies using raycasthit, and this worked for me up until I was shown weapon switching. However I’ve tried two different scripts on weapon switching so I’m sure the issue is with my melee system script. No errors come up in the script, and the “distance” variable only updates on game load, but not when I attack (thus resulting in enemies losing no health) I’ve been stressing over this for a few days trying to fix it myself, to no avail.
Here’s the script, I’m sure it’s something obvious:
var maxdistance : float = 1.5;
var axe: Transform;
var fork_of_horripilation: Transform;
var thesystem : Transform;
var hit: RaycastHit;
function Update ()
{
if (Input.GetButtonDown("Fire1"))
{
//attack animation
axe.animation.Play("axeattack");
}
if (axe.animation.isPlaying == false)
{
axe.animation.CrossFade("axeidle");
}
if (Input.GetKey(KeyCode.LeftShift))
{
axe.animation.CrossFade("axerun");
}
if (Input.GetKeyUp(KeyCode.LeftShift))
{
axe.animation.CrossFade("axeidle");
}
if (Input.GetButtonDown("Fire1"))
{
//attack animation
fork_of_horripilation.animation.Play("forkattack");
}
if (fork_of_horripilation.animation.isPlaying == false)
{
fork_of_horripilation.animation.CrossFade("forkidle");
}
if (Input.GetKey(KeyCode.LeftShift))
{
fork_of_horripilation.animation.CrossFade("forkrun");
}
if (Input.GetKeyUp(KeyCode.LeftShift))
{
fork_of_horripilation.animation.CrossFade("forkidle");
}
}
//attack function
if (Physics.Raycast (thesystem.transform.position, thesystem.transform.TransformDirection(Vector3.forward), hit))
{
distance = hit.distance;
if (distance < maxdistance)
{
hit.transform.SendMessage("Applydamage", damage, SendMessageOptions.DontRequireReceiver);
}
}
<<<<<<<< start of floating chunk of code javascript is assuming is meant to be in the start function (not 100% on how js is responding here, but look at your bracketting)
Oh yes that’s working great now thankyou!! Though now when I click my enemy dies immediately even though enemy health is 100 and attacking only takes 3 health.
you’ve not posted the “ApplyDamage” function so can’t really help there yet, but as a shot in the dark make sure the greaterthan/lessthan are the right way around on the check against 0 health (my usual mistake there lol)
the if(health <= death) part? I tried a few things with it but it didn’t work, I tried making “death” a variable too rather than just putting 0, and defining both death and health as integers rather than just numbers, but that didn’t work either. (I’m quite new to Javascript as you can probably tell, I’ve just come from using Python, so again it’s probably a silly mistake)
Thanks again!
#pragma strict
var health: int = 100;
var death: int = 0;
function Update ()
{
if(health <= death)
{
dead();
}
}
function Applydamage (damage : int)
{
health -= damage;
}
function dead()
{
Destroy (gameObject);
}