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 Mman1235 · Sep 09, 2015 at 07:06 PM · c#movement scriptfreezevector3.movetowards

Unity crashes on event of specific code

I am working right now on the movement in a sort of turn-based game and am almost done configuring it, I have this code, but I want something more complex so I could see the movement occur instead of the player just jumping to the position he needs:

 rb.MovePosition(Vector3.MoveTowards(Player.transform.position,BattleGrid[2,1].position,200f));

it works, but I want to see the movement occur and smoothly, so I made this code to replace the previous code:

 float sqrRemainingDistance = (Player.transform.position - BattleGrid[2,1].position).sqrMagnitude;
 while(sqrRemainingDistance > 0)            {
             Vector3 newPostion = Vector3.MoveTowards(rb.position, BattleGrid[2,1].position, inverseMoveTime * Time.deltaTime);
             rb.MovePosition (newPostion);
             sqrRemainingDistance = (Player.transform.position - BattleGrid[2,1].position).sqrMagnitude;

The issue is, this code makes the entire unity freeze up while playing, just when I do the thing that activates this code (or the previous code when I tried earlier).

There isn't any error or warning popping up... Does anyone know why this code makes the unity freeze up???

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

2 Replies

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

Answer by Eudaimonium · Sep 10, 2015 at 11:15 AM

Problem is, your While loop never finishes.

Explanation why: Distance between two points, which is your distance, is never less than zero. Your "destination" is never precisely hit. Suppose your player is at one frame 0.1 distance from destination, and it moves at 0.07 units per frame.

Next frame he will be 0.03 units from the destination.

Next frame he will not be -0.06 units away. He will be 0.06 units away. Distance is never negative.

How to solve this:
Idea #1: Within the code that initiates the move command (a click or some other input, I assume), aside from starting the loop that moves the player, also first remember the initial distance the player has to travel.

Then, within the loop that moves the player, keep track of distance traveled so far. Once the distance traveled so far is equal or larger than initial distance that needed traversing, stop the player.

Idea #2: This is a bit quicker but less robust solution:
Instead of checking if sqrRemainingDistance is larger than zero, simply check if it's larger than some fine tuned value. For example, move if distance is > 0.5, or such.

Additional problem: Even if your While loop were to finish normally, you would STILL see the player teleport to the destination.

This is because the Render of the scene happens once per frame, once all scripts have finished their execution. You have calculated and moved your player the entire distance within one While loop that executes fully within that one frame.

Instead of While loop, simply use the Update function of your script. This function executes one per frame. So, move your code from our while loop, into the Update loop (without While or other own loops). That way, each frame (each Update pass) your player will move for some small distance.

Code example

 public Vector3 playerDestination;
 public bool NeedsTravelling;
 
 //[...]
 //At the event that starts to move the player:
 playerDestination = positionOnYourGridThatYouNeedToMoveTo;
 NeedsTravelling = true;
 
 //[...]
 
 public void Update()
 {
    if (NeedsTravelling)
   {
       Vector3 actualDistance = playerDestination - Player.transform.position;
 
       if (actualDistance.sqrMagnitude > 0.5)
       {  //Note: You will need to calibrate the Speed value, since it's used differently now, it will have to be some small value such as 0.01 or so I assume.
          actualDistance.Normalize();
           Player.transform.position += actualDistance * speed;
       }
       else
       {  //If we're close enough, do not travel anymore:
          NeedsTravelling = false;
       }
   }
 }

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 Mman1235 · Sep 10, 2015 at 04:39 PM 0
Share

Thanks for the feedback :D

I implemented this to work with how the game works and it is just as I need it to be, so thanks a lot for the creative idea (knew it had something to do with endless loop couldn't figure out how to fix thx)!

avatar image Mman1235 · Sep 10, 2015 at 04:59 PM 0
Share

I am encountering the last issue to fix th movement entirely though

I need the game to wait for a few seconds till beginning to do the movement which works great.

Problem is I can't use WaitForSeconds to do this in update nor in an IEnumerator.

Do you know by any chance a way to make it happen? thanks!

avatar image Mman1235 · Sep 10, 2015 at 06:31 PM 0
Share

Never $$anonymous$$ind fixed it myself

avatar image
0

Answer by Wiebren-de-Haan · Sep 09, 2015 at 07:16 PM

I'm not an expert on explaining things, and my english is not great, but i will try to explain it :)

If you do this in a while loop, it wants to do this in that one frame it starts in. And the code only continuous when the sqrRemainingDistance == 0. This will loop infinite (can't explain why).

You can try to use this: Vector3 newPosition = Vector3.Lerp(rb.position, BattleGrid[2, 1].position, Time.deltaTime * speed); rb.position = newPosition;

Then you just check with an if statement if the rb.position == BattleGrid[2, 1], and if that is correct, you can continu with other stuff

Comment
Add comment · 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

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

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Movement Jumping Help 0 Answers

A* freezes unity 1 Answer

ReadLine from Named Pipe freezes Unity 1 Answer


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