- Home /
translate a melee attacking C# code to JavaScript (to be sure if I did it right and help with delay between each hit)
I saw the Hack & Slash Toutorial to make a melee attacking enemy. The toutorial is on C# but I use JavaScript so I tried to translate the melee attack code from C# to JS. This is what I did so far:
var distance; var target : Transform; var moveSpeed = 12.0; var damping = 6.0; var attackTimer : float; var coolDown : float;
function Start () { attackTimer = 0; coolDown = 2.0f; }
function Update ()
{
distance = Vector3.Distance(target.position, transform.position);
lookAt ();
if (lookAt)
{
attack ();
}
}
function lookAt () { var rotation = Quaternion.LookRotation(target.position - transform.position); transform.rotation = Quaternion.Slerp(transform.rotation, rotation, Time.deltaTime * damping); }
function attack () {
transform.Translate(Vector3.forward * moveSpeed *Time.deltaTime);
if (distance < 2.5f)
{
CharacterControl.healthBarDisplay -= 0.1;
}
}
The only problem is the delay between each hit - the dealy is so small that I getting dead in half a second (if you think the problem is that I loss only 0.1% of my lives so I don't see it in the game, you wrong. 100% of my lives = 1, so 0.1% means 10% of my lives). I know I didn't set the delay, I just don't know how to translate that part to JS. can someone Help me do that? The only problem is the delay.
Sorry for poor english and many stupid questions I asked before... :)
Answer by FLASHDENMARK · May 14, 2011 at 07:19 PM
var distance; var target : Transform; var moveSpeed = 12.0; var damping = 6.0; var canAttack = true; var coolDown : float;
function Start () { attackTimer = 0; coolDown = 2.0f; }
function Update ()
{
distance = Vector3.Distance(target.position, transform.position);
lookAt ();
if (lookAt)
{
attack ();
}
}
function lookAt () { var rotation = Quaternion.LookRotation(target.position - transform.position); transform.rotation = Quaternion.Slerp(transform.rotation, rotation, Time.deltaTime * damping); }
function attack () {
transform.Translate(Vector3.forward moveSpeed Time.deltaTime);
if (distance < 2.5f && canAttack)
{
CharacterControl.healthBarDisplay -= 0.1;
canAttack = false;
yield WaitForSeconds(coolDown);
canAttack = true;
}
}
Ty, it works! There was not much things to change, now I know I did good.
Your answer
Follow this Question
Related Questions
Camera Effect + Translating US to C# 3 Answers
Move from point A to point B, then destroy? ( java S) 2 Answers
Translate Into JavaScript Please 1 Answer
Android touch is not dynamic 1 Answer
translate C# with => to unityscript 2 Answers