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 jearen · Aug 29, 2013 at 05:53 PM · positionvector3distancefollowstart

Return to Start Position

Im trying to make my enemies return to original point of spawn when the player is more than 25 units away, however, i suspect there´s a slight error somewhere in my method as it does NOT waddle back to it´s point of origin, and just sits there staring into my soul with it´s empty cold eyes.

 var target : Transform;
 var moveSpeed = 20;
 var rotationSpeed = 5;
 var myTransform : Transform;
 var follow : boolean = true;
 private var startPosition : Vector3;
 var ReturnToPosition : boolean = false;
  
 function Awake()
 {
 myTransform = transform;
 }
  
 function Start()
 {
 startPosition = transform.position; //store the start position
 target = GameObject.FindWithTag("Player").transform;
  
 }
 

  
 function Update () { 
 var distance = Vector3.Distance(target.position,myTransform.position); 
      if(distance < 2){ follow = false; } 
      if(distance > 2){ follow = true; }
      if(distance > 25){ follow = false; } 
      if(follow == true) { myTransform.rotation = Quaternion.Slerp(myTransform.rotation,Quaternion.LookRotation(target.position - myTransform.position), rotationSpeed*Time.deltaTime);
     myTransform.position += myTransform.forward * moveSpeed * Time.deltaTime;
        if(distance > 25) { ReturnToPosition = true; }
        if(ReturnToPosition == true) {
 //rotate to look at the start position
 myTransform.rotation = Quaternion.Slerp(myTransform.rotation,
 Quaternion.LookRotation(startPosition.position - myTransform.position),rotationSpeed*Time.deltaTime);
  
 //move back to start position
 myTransform.position += myTransform.forward * moveSpeed * Time.deltaTime;
 }

    
    
  
  
 }

}

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 perchik · Aug 29, 2013 at 05:57 PM 0
Share

I'd suggest putting in some Debug.Log statements showing what case you are in or what code block you are in. Then you can see if you're ever getting past if(ReturnToPosition == true)

avatar image Matthew012 · Aug 29, 2013 at 06:34 PM 0
Share

Also look at the boolean as you run the game to see if it is changing. That tool is for you to edit while in game to debug and test.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Hoeloe · Aug 29, 2013 at 06:50 PM

Some of this code seems pointless. Why are you use "myTransform", and not the built-in "transform" that's given to you? Also, you're testing for distance > 25 twice, which is a waste, and you should probably use else statements (for example, if(distance < 2)... else if(distance > 2) makes more sense, because distance can only be bigger than 2 if it's not less than 2, and it saves a comparison).

As for your problem, it's actually fairly obvious. The key lines are these:

 if(distance > 25){ follow = false; } 

and

 if(follow == true) {

First of all, when the player walks out of range, you're setting follow to false. That's good, because it stops the enemy from following the player... but have a look at what else it does. Remember that, when you use an if statement, it acts on all the code within the { }, so let's have a look at what code is inside the brackets for if(follow == true).

  if(follow == true) 
  { 
      myTransform.rotation = Quaternion.Slerp(myTransform.rotation,Quaternion.LookRotation(target.position - myTransform.position), rotationSpeed*Time.deltaTime);
      myTransform.position += myTransform.forward * moveSpeed * Time.deltaTime;

      if(distance > 25) { ReturnToPosition = true; }
      if(ReturnToPosition == true) 
      {
          //rotate to look at the start position
          myTransform.rotation = Quaternion.Slerp(myTransform.rotation, Quaternion.LookRotation(startPosition.position - myTransform.position),rotationSpeed*Time.deltaTime);
  
          //move back to start position
          myTransform.position += myTransform.forward * moveSpeed * Time.deltaTime;
      }
 }

(I've reformatted it slightly to make it easier to read).

Now, we can see that all the code to return the NPC to the start point is within this if statement... but you set "follow" to false earlier in the code, meaning that this code will never be run when the distance is greater than 25, meaning that "ReturnToPosition" is never set, and thus the rest of the code isn't run. You just need to take the last bit out of the "follow" condition.

Comment
Add comment · Show 5 · 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 robertbu · Aug 29, 2013 at 06:58 PM 0
Share

@Hoeloe -

Why are you use "myTransform", and not the built-in "transform"

Under the hood, Unity is doing a GetComponent() call when you access 'transform', so caching the transform is a good thing.

avatar image Hoeloe · Aug 29, 2013 at 07:00 PM 0
Share

Huh. I assumed they cached it already. Seems a bit mental.

avatar image robertbu · Aug 29, 2013 at 07:09 PM 0
Share

That was my initial assumption also.

avatar image jearen · Aug 29, 2013 at 07:10 PM 0
Share

Thank you for your snappy answers, i'll try to fix my code and report back when/if i can get it to work =)

avatar image Hoeloe · Aug 29, 2013 at 11:40 PM 0
Share

I recommend, by the way, putting curly braces on their own separate lines, and indenting each pair as you go. It makes it more readable, and easier to avoid errors like this, as the boundaries between code groups are easier to see.

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

20 People are following this question.

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

Related Questions

Camera rotation around player while following. 6 Answers

Find position of point beetween two objects on certain distance 1 Answer

Enemy Jitters when i want him to stop a certain distance 0 Answers

Vector3.Project, how to? 2 Answers

how do i fix my script in the content below? 0 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