Search Unity

3D Third Person Jump Movement code bug

Discussion in 'Physics' started by GazingUp, Jan 21, 2020.

  1. GazingUp

    GazingUp

    Joined:
    Jun 16, 2015
    Posts:
    271
    I can't figure out what's causing my cube to fly off to the sky and outside of the frustum view.

    Code (CSharp):
    1.  
    2. var velocity_y = 0f;
    3.         if(body.velocity.y < -speed+2f)
    4.         {
    5.             velocity_y = -speed+2f;
    6.         }
    7.         else
    8.         {
    9.              velocity_y = body.velocity.y;
    10.         }
    11.  
    12. if (jump)
    13.         {
    14.             body.AddForce(0, 300, 0);
    15.             jump = false;
    16.         }
    17.         else
    18.         {
    19.             body.velocity = (Quaternion.Euler(0, cam.transform.eulerAngles.y, 0) * new Vector3(h, velocity_y, v) * speed);
    20.  
    21.         }
    Above code is in FixedUpdate. My jump bool is turned true in the Update when space is pressed with getkeydown.

    This is what happens to my cube guy:



    It flies off to the sky forever! I've been trying all kinds of combinations. I tried doing the body.velocity = Vector3.up * speed thing and adding it to the body.velocity, still flies off. What's causing it to fly off?
     
  2. dgoyette

    dgoyette

    Joined:
    Jul 1, 2016
    Posts:
    4,195
    Well, it kind of looks like on line 19 there, you're just increasing velocity every frame after you start having even a small amount of upward velocity. You're basically multiplying the current velocity by speed, and assigning that as the new velocity. If you comment out that line, does the problem go away?

    As a potentially bigger issue, it's somewhat concerning to see you using both AddForce and manually setting velocity. It's usually one or the other, except for very specific cases.
     
  3. GazingUp

    GazingUp

    Joined:
    Jun 16, 2015
    Posts:
    271
    Thank you for your response! And yes you're totally right! I forgot to mention that. But I did try to change velocity also by adding some speed to the Vector3.up.
    When I remove line 19 it totally works but I want movement while jumping. But line 19 really works well on its own in that it has really smooth movement with a complete 180 degree rotation and everything. How do I include that movement along with jumping? I still don't quite understand why it flies to infinity upwards because when im moving, velocity.y is zero.
     
  4. dgoyette

    dgoyette

    Joined:
    Jul 1, 2016
    Posts:
    4,195
    It's going to infinity, I believe, because every Fixed Update, it's multiplying the velocity by the Speed. Even if the speed is not very large, the velocity will just keep growing faster and faster. For example, after you jump, let's say your velocity is (0, 10, 0), because you're moving up. And let's say the Speed you're using is 5. After the next fixed update, the velocity will be (0, 50, 0), then after the next FixedUpdate it will be (0, 250, 0), because you just keep multiplying by speed. Eventually the velocity will be infinite. You're not seeing this as a problem when you're on the ground because you're multiplying the speed by 0, since vertical velocity is 0 in that case.

    Anyway, I figure you can fix this by not adjusting the y component of the velocity on line 19. Right now you have this:

    body.velocity = (Quaternion.Euler(0, cam.transform.eulerAngles.y, 0) * new Vector3(h, velocity_y, v) * speed);


    Maybe something like this:

    Code (CSharp):
    1. var tempVelocity = (Quaternion.Euler(0, cam.transform.eulerAngles.y, 0) * new Vector3(h, 0, v) * speed);
    2. body.velocity = new Vector3(tempVelocity.x, velocity_y, tempVelocity.z );
    I'm not 100% sure that works, but the idea would be to just pass through the y-component of the velocity, so that this code doesn't affect it at all.
     
    GazingUp likes this.
  5. GazingUp

    GazingUp

    Joined:
    Jun 16, 2015
    Posts:
    271
    Thank you very much. I knew posting here would help me with an extra pair of eyes. This is a classic programming problem. I totally forgot about the velocity_y being updated right in the body.velocity! Damn. I should get used to using temp variables. I did it for rotation since I read not to directly change any of the transforms.
    After implementing the temporary velocity variable it looks like this now:



    Thank you!
     
    dgoyette likes this.
  6. dgoyette

    dgoyette

    Joined:
    Jul 1, 2016
    Posts:
    4,195
    Looks good. :)

    By the way, just judging from the texture, it looks as though you've created a ProBuilder plane, and then changed the scale of the plane to be something other than (1,1,1). While that's okay, I'd recommend generally using the Face/Edge tools to change the size of PB objects, rather than scaling them. It will keep your textures at high resolution. And nevermind/sorry if I'm seeing that wrong. :)
     
  7. GazingUp

    GazingUp

    Joined:
    Jun 16, 2015
    Posts:
    271
    Good eye. You are right. I did scale it up. I can increase size of PB objects using face/edge tools? I wasn't aware of that! :) Again thank you.
     
  8. dgoyette

    dgoyette

    Joined:
    Jul 1, 2016
    Posts:
    4,195
    Yeah, that's really the main strength of ProBuilder, being able to move/extrude individual faces to build up your geometry in-editor. It does the magic to keep the UVs at the same resolutions so things don't look stretched.
    PB.gif
     
    tjmaul likes this.