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 darthbator · Oct 22, 2013 at 06:49 PM · c#crashjumprigidbody.moveposition

This jump code crashes the editor!

So I started wiring together a jump method for the 2D game that I've been working on. Recently I've moved away from moving my characters by altering their transform to using rigidbody.movePosition. Everything was working perfectly until I started messing with jump. I've isolated this down to just a simple example of the code that's creating the crash. I'm not entirely sure why this is happening. Any help would be most appreciated!

 public float jumpHeight;
 
 void Update () {
     if (Input.GetKeyDown(KeyCode.Space)) {
         jump();
     }
 }
 
 void jump () {
     Vector3 jumpedFrom = transform.position;
     
     while (tranform.position.y < jumpedFrom.y + jumpHeight)
         rigidbody.movePosition(transform.position + Vector3.up * Time.deltaTime);
 }
Comment
Add comment · Show 4
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 meat5000 ♦ · Oct 22, 2013 at 06:57 PM 1
Share

It gets stuck in an infinite while loop I believe.

avatar image darthbator · Oct 22, 2013 at 07:00 PM 0
Share

That was my thought as well, I just don't understand why. If I take the loop out it does correctly move the character. I see the editor immediately crash on me. I never even see the character move with the loop inserted.

avatar image meat5000 ♦ · Oct 22, 2013 at 07:07 PM 0
Share

$$anonymous$$aybe start by placing some braces around it?

 while(condition)
 {

 }

Then add a Debug.Log within the loop to see if

 transform.position + Vector3.up * Time.deltaTime

is actually changing.

avatar image darthbator · Oct 22, 2013 at 08:09 PM 0
Share

The actual code has braces there, only my example does not. I have added debugging to the loop however the editor seems to crash before the debug prints to log.

1 Reply

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

Answer by Tomer-Barkan · Oct 22, 2013 at 09:02 PM

The problem is you're putting it in an endless loop, since rigidbody.MovePosition() doesn't actually change transform.position, it tells the physics engine to move the object. That is only done the next time the physics engine does its run.

Since the transform.position is not changed by the command:

 rigidbody.movePosition(transform.position + Vector3.up * Time.deltaTime);

The while condition (tranform.position.y < jumpedFrom.y + jumpHeight) is never false, hence an endless loop, and the physics engine never gets its turn to run and modify the position.

Also, the while is running in a single frame, so even if it had finished, it would have moved the object all the way to the top in a single frame, which is probably not what you intended.

Here's an alternative:

 public float jumpHeight;
 public float targetHeight; // set to the final height of the jump
 public bool jumping = false; // set true when moving upwards
 
 void Update () {
     if (Input.GetKeyDown(KeyCode.Space)) {
         targetHeight = transform.position.y + jumpHeight; // set jump height
         jumping = true; // start jump
     }
 
     // if moving upwards, run the jump
     if (jumping) { 
         Jump();
     }
 }
 
 void jump () {
     // if reached target, set jumping to false to stop moving upwards
     if (transform.position.y > targetHeight) {
         jumping = false;
         return;
     }
 
     // move object upwards
     rigidbody.MovePosition(transform.position + Vector3.up * Time.deltaTime);
 }
Comment
Add comment · Show 6 · 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 meat5000 ♦ · Oct 22, 2013 at 09:08 PM 0
Share

BTW, $$anonymous$$ovePosition is with capital $$anonymous$$.

Good catch. I'm guessing that is the cause of the crash. Repeat No $$anonymous$$ethod errors wildly looping to infinity.

I didn't even notice the $$anonymous$$! Let's see what OP reports :)

avatar image Tomer-Barkan · Oct 22, 2013 at 09:11 PM 0
Share

Yeah but I'm guessing it's just a copy-paste mistake, since it wouldn't even compile...

avatar image meat5000 ♦ · Oct 22, 2013 at 09:21 PM 0
Share

I think it compiled, I imagine it crashed on Play. Anyhoo, we shall see :)

avatar image darthbator · Oct 22, 2013 at 09:54 PM 0
Share

I'll need to let you guys know a little later on. I actually don't have the project at this location. I am sure the $$anonymous$$ovePosition method is correctly cased in the actual code as I let intellisense complete basically everything. I was writing this from memory straight into the text box. it would be a miracle if that was the only mistake. Thanks so much for the help both of you! I'll make sure to update this when I have access to the project again later on today.

avatar image meat5000 ♦ · Oct 22, 2013 at 09:57 PM 0
Share

The bottom line, I guess, is don't use while() for this kind of thing. Find another way. tbkn has offered one above.

Show more comments

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

16 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

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Teleportation script causing crash? 2 Answers

UDP with coroutines crashing unity? 1 Answer

WebCamTexture IOS crashing? 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