I'm wanting to create a 2D platform game with physics based character control. I have tried the Character Controller component which does work really well for the most part but my character is spherical in shape and I would like to take advantage of physics when rolling down curved slopes etc. I have started coding and I'm running into a problem with jumping. I want my character to jump in a perfect arc unless the player changes direction in the air but the basic jump method from the reference doesn't do that. I'm sure others have seen it but basically the ball slows abruptly as it jumps up then speeds up as it falls back down.
Here is my code,
Any help would be great.
p.s. I am also open to suggestions on how to use the character controller along with rigidbody if there is a way to seamlessly switch between the two as needed. I did try that option though with no success.
asked Aug 26 '10 at 06:33 PM
I'm not sure about switching between the character controller and rigidbody. It's really either one or the other.
Using a CharacterController
Character controllers are meant to handle collisions without involving the physics-y stuff. You could calculate all movement yourself if you wanted, foregoing the physics stuff, in which case, you could get something similar by actually calculating the speeds and movements. Take a look at the 3DPlatformTutorial's controller script which applies gravity and forward motion while jumping.
Using a rigidbody
You probably want something like:
Your problem was that you were setting velocity, not adding to it. If you had even had
I think maybe you just described it poorly or else the code is still wrong. A perfect arc would have the same radius from it's center point at either end and the same curve to reach either end. If you were following actual physics, without additional force being applied in the forward direction (do you have a fan or jetpack pushing you forward?), wind resistance (drag) would naturally slow you down, which is why moving jumps have a sharper fall than their initial rise and they do not follow a perfect arc.
Because your code accelerates based on horizontal input,
In order to ensure a perfect arc, you would need to maintain a constant horizontal velocity, meaning that you would need to either adjust the speeds, or disable drag during the jump if it is on and prevent the user's input from affecting movement while jumping.
If a perfect arc in your definition is a perfect semi-circle, the your motion will have the same speed, but will move in a direction moving around in a circle. Essentially you'd have something like take Vector3.Up and rotate it by a number of degrees relative to the size of your arc in the direction that you are facing every update until it was equal to or past -Vector3.up and at each update change your velocity to be that Vector3 multiplied by some constant speed value.
I have a physics equation here that was made for firing a projectile in an arc towards a target. I'm not sure that the arc will be a perfect 180 degree arc, but you can give it a shot.
Here is the equation:
You'll need the following information:
1 will have to be calculated, and 2 can be a tweakable value inside of the editor.
So, let's say you want your jump to go 5 meters. You'd have this:
After you have those, you rotate your player like this:
And then launch it in the air!
answered Aug 27 '10 at 04:41 AM