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 CryoGen14 · Jun 07, 2020 at 01:56 PM · scripting problemdistanceloopcharacter controllermovetowards

Make player move to location and stop within a certain distance

Hello everyone,

So I have a situation where the player presses an UI button and I need the player to move to the target and stop when within a certain distance, I tried numerous ways using while loops or for loop usually resulting in the editor freezing, which means the exit condition is never being met.

The piece that worked the best was the do and while loop:

  if (option == 1)
         {
             float distance = Vector3.Distance(transform.position, combatManager.Target.transform.position);
             distance = Mathf.RoundToInt(distance);
             Debug.Log(distance.ToString());
             do
             {
                 Vector3 mdir = (transform.position - combatManager.Target.transform.position);
                 //playerAnim.SetTrigger("CombatStandard");
                 _charController.Move(-mdir * 3 * Time.deltaTime);
                 Debug.Log("moving");
                 distance = Vector3.Distance(transform.position, combatManager.Target.transform.position);
             } while (distance > 5);
         }

While this does work in moving the player closer, it does so instantly instead of over time.

any ideas?

here is a gif of whats happening:

https://imgur.com/a/ZllwX9l

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

Answer by GREYSETH · Jun 07, 2020 at 02:26 PM

@CryoGen14 Can you please be more specific on what you're trying to do? Because I might have a solution to your problem.

Comment
Add comment · Show 3 · 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 CryoGen14 · Jun 07, 2020 at 02:44 PM 0
Share

I want to have the player attack the currently assigned target when the player presses the attack button in the UI.

When the player presses the button, I want to have the player model(A) move from its current position towards the enemy(B), and then stop when it is within a certain distance of it.

I am trying to achieve this by checking the distance between A- and B and then moving A toward B until it reaches the distance specified, and the loop will end allowing the script to continue.

avatar image CryoGen14 · Jun 07, 2020 at 03:00 PM 0
Share

I have added a gif to the original post so you can see whats happening

avatar image GREYSETH · Jun 07, 2020 at 04:06 PM 1
Share

I don't know how your script could look so complex but this is what I'm thinking :

 float $$anonymous$$Distance = 5;
 float distance;
 
 void Update ()
 {
     // set distance variable to player and target position
     if (option == 1)
     {
         if (distance >= $$anonymous$$Distance)
         {
             controller.$$anonymous$$ove(target.transform.position * speed * Time.deltaTime);
        }else 
        {
             // What you want to happen when the player is close
         }
     }

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

230 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 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 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 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 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 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 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 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 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 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 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

My Animation Loops 3/4 of the way through 3 Answers

Another function for activating a different script 0 Answers

How to check if the difference between any pair of numbers in an array is less than some value? 0 Answers

OVR Player Controller issuse with unity 5.5.0f3 2 Answers

Player keeps flying up 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