- Home /
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);
}
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.
$$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.
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.
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);
}
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 :)
Yeah but I'm guessing it's just a copy-paste mistake, since it wouldn't even compile...
I think it compiled, I imagine it crashed on Play. Anyhoo, we shall see :)
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.
The bottom line, I guess, is don't use while() for this kind of thing. Find another way. tbkn has offered one above.
Your answer
Follow this Question
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