Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 Persona · Jul 01, 2011 at 07:12 AM · javascriptmovementfleeing

A flaw in my 2d chase or flee script

I've written the following code in Javascript in order to get an enemy to run away if underleveled, or chase if stronger. The problem is that the fleeing part of the script stops a short distance away and won't move if the player gets close. Can anyone point out why:

 var Waypoint : Transform[];
 var speed : int = 20;
 private var currentWP : int;
 var loop : boolean = true;
 var isChase: boolean = false;
 var isRun: boolean = false;
 
 // Our target
 var Target: Transform;
 //How slow we are
 var Damp: float = 1.0;
 //Are we out leveled?
 var Outleveled: boolean = false;
 //How close to detection
 var detectionRange: int = 5;
 
 var Height: float = 0;
 
 
 function Awake(){
 Height = transform.position.y;
 }
 
 function Update() {
 transform.position.y = Height;
 
 var FromPlayer: Vector2 = Vector2(Target.position.x - transform.position.x, 0);
 var RunPlayer: Vector2 = Vector2(transform.position.x - Target.position.x, 0);
 
 
 if(FromPlayer.magnitude <= detectionRange){
 if(Outleveled){
 isRun = true;
 }
 
 else{
 isChase = true;
     }
 }
 if(FromPlayer.magnitude >= detectionRange){
 isChase = false;
 isRun = false;
 }
 
 
 if(!isChase){
 if(currentWP < Waypoint.length){
 
 var target = Waypoint[currentWP].position;
 var moveDirection : Vector2 = target - transform.position;
 
 var velocity = rigidbody.velocity;
 
 if (moveDirection.magnitude < 1){
 currentWP++;
 }
 else {
 velocity = moveDirection.normalized * speed;
 }
 }
 else{
 if(loop){
 currentWP=0;
 }
 else{
 velocity = Vector2.zero;
         }
     }
 
 rigidbody.velocity = velocity;
 }
 
 if(isChase){
 Chase();
 }
 
 if(isRun){
 RunAway();
 }
 
 }
 
 
 
 function Chase(){
 
 var rotate = Quaternion.LookRotation(Target.position - transform.position, Vector3.up);
 transform.rotation = Quaternion.Slerp(transform.rotation, rotate, Time.deltaTime * Damp);
 transform.position = Vector2.Lerp(transform.position, Target.position,Time.deltaTime * Damp);
 }
 
 
 
 function RunAway(){
 
 var rotate = Quaternion.LookRotation(transform.position - Target.position, Vector3.up);
 transform.rotation = Quaternion.Slerp(transform.rotation, rotate, Time.deltaTime * Damp);
 transform.position = Vector2.Lerp(transform.position, transform.position - Target.position,Time.deltaTime * Damp);

}

Comment
Add comment · Show 2
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 Persona · Jul 04, 2011 at 05:07 PM 0
Share

I don't think the waypoint system is interfering, does anyone have suggestion?

avatar image Chris D · Jul 04, 2011 at 05:25 PM 0
Share

Put a couple of print() or Debug.Log() statements in there to see which parts of your code are reached and which aren't.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by DaveA · Jul 04, 2011 at 07:16 PM

It may be possible that isRun and isChase are both true at the same time? Try setting them opposite of each other where currently one is set true (set the other false).

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 Persona · Jul 05, 2011 at 12:43 AM 0
Share

I did but it's more of the same. I think the problem is the Run Away function function. It runs only once, but once it stops, it doesn't work properly again.

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Slowing Down When Sliding Across Walls 2 Answers

Horizontal axis not working after animation 1 Answer

Move Object Smoothly 1 Answer

Rigidbody.MovePosition doesn't move reliably? 1 Answer

car movement basics 5 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