- Home /
What have I done wrong with gravity and movement?
Hi, so I've been having a couple of problems with this script. I got player movement working and have been trying to add a jump.
The first problem is the jump isn't smooth. The object shoots up in what seems like 1 frame and then falls down.
Second is that the jump doesn't work when standing still.
A couple of things I've managed to figure out is that when standing still the controller doesn't read as grounded. Also that when i take out the Transform.translate line at the bottom it makes the controller be grounded when still, but then it can only move in x and z when off the ground. Thanks.
public float speeed = 8f;
private float verticalVelocity;
private float jumpForce = 40f;
private float gravity = 50f;
private Vector3 movement;
private Vector3 moveRotation;
void Update()
{
CharacterController controller = gameObject.GetComponent<CharacterController>();
float moveH = Input.GetAxis("Horizontal");
float moveV = Input.GetAxis("Vertical");
movement = new Vector3(moveH, verticalVelocity, moveV);
moveRotation = new Vector3(moveH, 0, moveV);
verticalVelocity = -gravity * Time.deltaTime;
//JUMP
if (controller.isGrounded)
{
print("CharacterController is grounded");
verticalVelocity = -gravity * Time.deltaTime;
if (Input.GetButtonDown("Jump"))
{
verticalVelocity = jumpForce;
}
}
//character rotation
if (moveRotation != Vector3.zero)
{
transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(moveRotation), 0.15f);
}
//move
// this line is what makes the controller not be grounded when standing still
transform.Translate(movement * speeed * Time.deltaTime, Space.World);
controller.Move(movement * speeed * Time.deltaTime);
}
I don't know what CharacterController.$$anonymous$$ove does, but is there a reason you're using it and Transform.Translate back to back?
Controller.move moves the controller, and stops when there's collisions.
I'm not sure how I ended up using translate.transform as well, but I think using translate.transform allows the player to move, ins$$anonymous$$d of constantly colliding with the floor.
For the I've now used raycast to detect the floor which works fine and now allows me to use jump when standing still. But my jump still teleports the player up and then gravity pulls it back down.
Answer by Jeye · Feb 23, 2019 at 07:03 PM
Rather than using isGrounded, I've used a physics.raycast to detect the floor which allows you to jump when standing still. But I'm still having problems with a smooth jump.
When the jump button is pressed the player teleport's upwards and the gravity gradually pulls them back down. Its probably something to do with how I've coded gravity.
Thanks for the help so far. Any more would be appreciated :)
Answer by James_BadAlchemy · Feb 12, 2019 at 01:28 AM
Have you tried just using unity's physics engine. By default unity applies realistic gravity.
Answer by umair94 · Feb 12, 2019 at 05:50 AM
i think if you just add rigidbody to the object . then jump on button down and change y-axis like if(Input.GetKeyDown(KeyCode.Space)){ transform.GetComponent().velocity = Vector3.up * speed; }
I've tried using a rigid body a couple of times but got worse results so reverted back to this. I'd like to try any figure out how to fix without rigid body. Not sure how possible that is though.
I may have used it incorrectly but I would do similar to this:
if (Input.GetButtonDown("Jump")) { rigidbody.velocity = Vector3.up * jumpForce; } Though just adding the rigid body to the game object makes the player very slow when on the ground and eventually clip through the ground. With no other force being applied.
(edit. shaking is from my camera script ignore that)
if you do something with physic I would highly recommend rigid body's
Your answer

Follow this Question
Related Questions
Charactercontroller won't go down, only goes up 1 Answer
controller.Move doesn't stay grounded when walking down slope 3 Answers
Strange Character Controller Behavior Caused by Simulated Gravity and Ground Check 0 Answers
why does character controller accelerate off ledges? 1 Answer
Change gravity point on trigger 0 Answers