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 Punkjim420 · Aug 08, 2012 at 03:43 AM · javascriptmovementdamageenemy aisend

incomplete Enemy Ai (move/attack on sight) js script

Im trying to make my enemy ai. I want it to run to me if im withing a set range from it(var chaseThreshold). I want it to stop running towards me, once it reaches the attack range(attackThreshold). Lastly, once it reaches the Attack range, i want it to send a message to the Player Object that deals damage.

When i test the script as is in unity, the enemy does not move. If i remove the bit telling my enemy to stop when it reaches attack range(attackThreshold) it moves towards my player and never stops, sometimes sends my player flying backwards on collision. I cant seem to find the proper way to my enemy to walk towards my player and stop when it reaches attack range at the same time. Also, my send message to apply damage does not get recieved. Help with either issue would be greatly apprieciated.

Enemy Ai.js:

var target : Transform; //enemies target var moveSpeed = 3; var rotationSpeed = 3; var attackThreshold = 10; //distance in which to attack var chaseThreshold = 20; //distance to chase var giveUpThreshold = 21; //how far you go before enemy quits chase var attackRepeatTime = 1; //attack delay var damage = 5; private var chasing = false; private var attackTime = Time.time; var myTransform : Transform; //current transform data of the enemy

function Awake(){ myTransform = transform; //cache transform data for easy access/performance }

function Start(){ target = GameObject.FindWithTag("Player").transform; //target the player }

function Update(){ var distance = (target.position - myTransform.position).magnitude; if(chasing) { myTransform.rotation = Quaternion.Slerp(myTransform.rotation, Quaternion.LookRotation(target.position - myTransform.position), rotationSpeed * Time.deltaTime);

     myTransform.position += myTransform.forward * moveSpeed * Time.deltaTime; //move to player
     
     if(distance > giveUpThreshold || distance < attackThreshold) { //give up if too far away
         chasing = false;
         }
     //attack if close and timer is ready
     if(distance < attackThreshold && Time.time > attackTime) {
         //attack: do your attack here.
         BroadcastMessage("PlayerDamage", damage, SendMessageOptions.DontRequireReceiver);
     attackTime = Time.time + attackRepeatTime; 
     }
     
     else {
     //not currently chasing
     //start chasing if target comes close
     if(distance < chaseThreshold) {
     chasing = true;
     
     }
 }

} } ***Player Status.js:*** //Goes on Player Object. public var playerCurHp : int; public var playerMaxHp : int; public var playerCurMp : int; // public var playerMaxMp : int; public var playerCurExp : int; public var playerMaxExp : int; public var playerLevel : int; var statusTexture: Texture; var hitDelay : float = 0.75; private var nextHitAllowed : float;

playerCurHp = 99999; playerMaxHp = 99999; playerCurMp = 99999; playerMaxMp = 99999; playerCurExp = 0; playerMaxExp = 250;

function PlayerDamage (damage : int){ playerCurHp -= damage;

 //Debug.Log(playerCurHp);

if(playerCurHp > playerMaxHp){ playerCurHp = playerMaxHp; } if(playerCurHp < 0){ playerCurHp = 0; } if(playerCurHp < 1){ //die // Die(); //Debug.Log("YOU DIED!"); }
//function Die () { //Application.LoadLevel (Application.loadedLevel);

}

function OnGUI(){ GUI.Label(Rect(50,10,150,100), statusTexture); GUI.Label(Rect(50,20,150,100), "HP " + (playerCurHp) + "/" + (playerMaxHp)); GUI.Label(Rect(50,35,150,100), "MP " + (playerCurMp) + "/" + (playerMaxMp));
}

Comment
Add comment · Show 1
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 Punkjim420 · Aug 09, 2012 at 04:29 AM 0
Share

i would still appreciate help. though iv changed the script a bit... i could update if anyone would like to see it, or use pastebin.

0 Replies

· Add your reply
  • Sort: 

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

7 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Move Camera According to Mouse Movement While Button is Pressed 1 Answer

Enemy Movement Problem! 1 Answer

UnityScript How to Detect Movement from FPS Controller 0 Answers

Line not working in C#? 1 Answer

Android OnMouseDown character movement 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