- Home /
Player Attack malfunction
I Have scripts that player send message dammage to enemies using raycast hit. but every player hit the enemies the functions didnt work. here the scripts
* MeleeScript.js (player script assigned on weapon object with mecanim, sending dammage message to enemies)
var TheDammage : int = 50;
var Distance : float;
var MaxDistance : float = 1.5;
var TheSystem : Transform;
var theAnimator : Animator;
function Update ()
{
if(Input.GetButtonDown("Fire1"))
{
theAnimator.SetBool("Hit01", true);
}
if(Input.GetButtonUp("Fire1"))
{
theAnimator.SetBool("Hit01", false);
}
if(Input.GetButtonDown("Fire2"))
{
theAnimator.SetBool("Hit02", true);
}
if(Input.GetButtonUp("Fire2"))
{
theAnimator.SetBool("Hit02", false);
}
}
function AttackDammage ()
{
//Attack function
var hit : RaycastHit;
if (Physics.Raycast (TheSystem.transform.position, TheSystem.transform.TransformDirection(Vector3.forward), hit))
{
Distance = hit.distance;
if (Distance < MaxDistance)
{
hit.transform.SendMessage("ApplyDammage", TheDammage, SendMessageOptions.DontRequireReceiver);
}
}
}
* EnemyHealth.js (default health of enemies, its reciever dammage from player)
var Health = 100;
function ApplyDammage (TheDammage : int)
{
Health -= TheDammage;
if(Health <= 0)
{
Dead();
}
}
function Dead()
{
Destroy (gameObject);
}
Even the distance between player and enemy very close, its still no happens
How to resolve the problem ? Thanks before.
Answer by ShadyD · Jan 04, 2014 at 10:16 PM
Hello,
Following the Brackeys Tutorial I see :), awesome tutorial serie imo.
Anyway, you're sending the message to the transform. Try changing
hit.transform.SendMessage(..
to
hit.gameObject.SendMessage(..
i have changed the code just like you suggest, but i got error. here the message from console
Assets/Script/$$anonymous$$eleeSystem.js(57,37): BCE0019: 'gameObject' is not a member of 'UnityEngine.RaycastHit'.
Oh wait, this is really silly. Didn't see it before. Your code was correct. Change it back to hit.transform.Send$$anonymous$$essage(.. Problem is, you just never call your AttackDammage() function.
Changing
if(Input.GetButtonDown("Fire1"))
{
theAnimator.SetBool("Hit01", true);
}
to
if(Input.GetButtonDown("Fire1"))
{
theAnimator.SetBool("Hit01", true);
AttackDammage();
}
should do the trick.
Oh yea, my bad. Its work, very helped. Thanks mate. But its make me confused, if you follow the tutorial from brackeys, he explained dat script, and also the function of attackdamage not call in update function, but the script absolutelly worked. What the different ?
Good to hear it works now :)! I never got that far with the brackey tutorials, I just followed a few (about 10 or so I think) to get started with Unity.
$$anonymous$$aybe download his project and compare your code. It could be that he accidently had a second (or finished) script working on the player. Link me to the video, I can take a look if you'd like.
Your answer
Follow this Question
Related Questions
Code don't function when 0 Answers
How can I attach a colldier that moves with the enemies sword in an attack animation? 0 Answers
How could I combine melee weapons and rays? 1 Answer
Weapon attack 0 Answers
noob question about melee combat 1 Answer