Hello folks. Sorry if this is the wrong place but I’m super new, and I’m having trouble with my character movement script… Whenever I jump, my character jerks upward as if instantly put at that position, instead of naturally moving. Plus, if I am moving forward while jumping, my character shoots forward as well. It makes jumping a rather jarring situation. Help please? Here is my code:
using UnityEngine;
using System.Collections;
public class Moveretry : MonoBehaviour
{
public float rotationSpeed = 5.0f;
public float forwardSpeed = 10.0f;
public float runSpeed;
public float jumpSpeed = 10.0f;
public float gravity = 20.0f;
private CharacterController charactercontroller;
private float bForward;
void Start ()
{
charactercontroller = GetComponent<CharacterController> ();
bForward = forwardSpeed;
}
void Update ()
{
transform.Rotate (0, (Input.GetAxisRaw ("Horizontal") *Time.deltaTime ) * rotationSpeed, 0);
Vector3 forward = transform.TransformDirection(Vector3.forward);
float speed = forwardSpeed * Input.GetAxis ("Vertical");
charactercontroller.SimpleMove (speed * forward);
if (Input.GetKey ("left shift") && Input.GetKey ("w") && charactercontroller.isGrounded)
{
forwardSpeed = runSpeed;
}
else
{
forwardSpeed = bForward;
}
if(Input.GetKeyDown ("space") && charactercontroller.isGrounded)
{
charactercontroller.Move(new Vector3 (0, jumpSpeed, 0));
}
charactercontroller.SimpleMove(Physics.gravity);
}
}
I apologize for how noobish it is. I’m still learning. Any advice would be appreciated!
I haven’t used CharacterController by myself, but have you read its documentation? It seems you should not call Move / SimpleMove multiple times per frame and SimpleMove already applies gravity. There are also examples in both functions.
And I bet there are tons of tutorials covering CharacterController.
For your concrete Problem, my best guess is, that you have to scale the up-Vector passed to characterController.Move by the deltaTime as used in the example docs.
charactercontroller.Move(new Vector3 (0, jumpSpeed * Time.deltaTime, 0));
But there may be more problems with your approach… (e.g. multiple calls to SimpleMove and additionally applying Gravity although SimpleMove is said to already handle gravity…)
Thank you. I got rid of the call to gravity. Unfortunately, when I scale the jumpspeed by Time.deltatime, my character still snaps up into place instead of smoothly moving up. :\
@Mattarias
I recently came up against this very issue, and the solution is to use a coroutine to create a smooth set of sequential upwards motions.
Code (CSharp):
void FixedUpdate() {
float yVal = 0;
if (jumping)
yVal = jumpSpeed;
moveDirection =
new Vector3(Input.GetAxis("Horizontal"), yVal, Input.GetAxis("Vertical"));
moveDirection = transform.TransformDirection(moveDirection);
.
.
.
doJump();
.
.
.}
Then in doJump() you can say jumping is true and start the coroutine which will keep jumping true for several frames.
Code (CSharp):
private void doJump() {
if (!jumping) {
jumping = true;
StartCoroutine(jumpUpForceOverTime());
}
}
IEnumerator jumpUpForceOverTime() {
for(int i = 0; i < 10; i++) {
yield return new WaitForFixedUpdate();
}
jumping = false;
yield return null;
I found 10 iterations to provide a smooth jump feel, but you could increase it for a longer jump, over more frames.
This is my character jumping and I am using a Character controller, no rigid body.
link: https://zippy.gfycat.com/LegalImpartialEidolonhelvum.webm