Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Michael 16 · May 14, 2011 at 06:14 PM · javascriptdelaytranslate

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... :)

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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;
  }

}

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Michael 16 · May 15, 2011 at 04:08 AM 0
Share

Ty, it works! There was not much things to change, now I know I did good.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

No one has followed this question yet.

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges