- Home /
Inconsistent jumping with character controller
I've made a simple first person player controller script, which just moves the character so far. The X-Z movement is working fine, but I'm having trouble with an annoying glitchy effect that can happen when jumping. Normally jumping there is no problem. However if I jump to a higher level platform from a lower one, the jumping becomes inconsistent. Every second bounce is very low, much lower than programmed, while the ones in between are fine. This continues until I let go of the space bar, at which point it seems to reset itself and all works well until I jump up a level again. Note that this does not happen when jumping down a level. Here is the code:
 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class Player : MonoBehaviour {
 
     public float speed;
     public float jumpSpeed;
     public float resetSpeed;
     public int health;
     public float gravity;
     Camera myCamera;
     Vector3 myCameraOrigin;
     Vector3 myCameraTarget;
     bool justStopped = false;
     float startTime;
     float journeyLength;
     public Vector3 currentVelocity;
 
     // Use this for initialization
     void Start () {
         myCamera = GetComponentInChildren<Camera> ();
     }
     
     // Update is called once per frame
     void Update () {
         currentVelocity.x = 0;
         currentVelocity.z = 0;
         CharacterController con = GetComponent<CharacterController> ();
         bool moving = false;
         if (Mathf.Abs (Input.GetAxisRaw ("Forward")) > 0) {
             moving = true;
             currentVelocity += Vector3.forward;
         }
         if (Mathf.Abs (Input.GetAxisRaw ("Backward")) > 0) {
             moving = true;
             currentVelocity += Vector3.back;
         }
         if (Mathf.Abs (Input.GetAxisRaw ("Left")) > 0) {
             moving = true;
             currentVelocity += Vector3.left;
         }
         if (Mathf.Abs (Input.GetAxisRaw ("Right")) > 0) {
             moving = true;
             currentVelocity += Vector3.right;
         }
         if (Mathf.Abs (Input.GetAxisRaw ("Jump")) > 0 && con.isGrounded) {
             currentVelocity.y += jumpSpeed * Time.deltaTime;
         }
         if (moving) {
             float velocitySpeed = speed * Time.deltaTime;
             Vector3 moveDirection = myCamera.transform.TransformDirection (currentVelocity);
             moveDirection.y = 0;
             moveDirection.Normalize ();
             moveDirection *= velocitySpeed;
             currentVelocity.x = moveDirection.x;
             currentVelocity.z = moveDirection.z;
             justStopped = false;
             //myCamera.transform.Translate (Vector3.left * 0.01f * Mathf.Sin (Time.time * 5));
             myCamera.transform.Translate (Vector3.up * 0.01f * Mathf.Sin (Time.time * 10));
         }
         if (!con.isGrounded) {
             currentVelocity.y -= gravity * Time.deltaTime;
         } else if (currentVelocity.y < 0) {
             currentVelocity.y = 0;
         }
         con.Move (currentVelocity);
         if (!moving && !justStopped) {
             justStopped = true;
             myCameraOrigin = myCamera.transform.localPosition;
             myCameraTarget = new Vector3 (0, 1, 0);
             startTime = Time.time;
             journeyLength = Vector3.Distance(myCameraOrigin, myCameraTarget);
         } else if (!moving) {
             float distCovered = (Time.time - startTime) * resetSpeed;
             float fracJourney = distCovered / journeyLength;
             myCamera.transform.localPosition = Vector3.Lerp (myCamera.transform.localPosition, myCameraTarget, fracJourney);
         }
         transform.rotation = Quaternion.Euler (0, myCamera.transform.rotation.y, 0);
     }
 }
Answer by GoodKingJohn · Jul 06, 2017 at 03:46 PM
I solved the issue by moving the jump check to add jumping force after the isGrounded check, so that if a jump was initiated it is not immediately negated, as I think is what was happening.
Your answer
 
 
             Follow this Question
Related Questions
Help with Character Controller Platform Physics 0 Answers
Gun collision problem 3 Answers
Non slippery movement 1 Answer
Character Physics 0 Answers
 koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                