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);
}