Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Join us on Dec 8, 2022, between 7 am & 7 pm EST, in the DOTS Dev Blitz Day 2022 - Q&A forum, Discord, and Unity3D Subreddit to learn more about DOTS directly from the Unity Developers.
    Dismiss Notice
  3. Have a look at our Games Focus blog post series which will show what Unity is doing for all game developers – now, next year, and in the future.
    Dismiss Notice

Question Character Controller Jump teleports instead of going slow up. What to do?

Discussion in 'Scripting' started by unity_FUc_6LWWe7c3mw, Nov 24, 2022.

  1. unity_FUc_6LWWe7c3mw


    Jan 1, 2021
    Good day,
    i have a problem in Unity, and i really hope someone can help me out ;).
    So i momently pogramm a Third Person Controller with a characterController component and the movement & animations ... al worked fine out. But then at the Jump i got stuck because i knew i can move it on the y Value but i dont realy got it working with the gravity so i searched on YT and found a tutorial that says how you can pogramm the gravity... (When not grounded set it to something like -9.8 ...) And then i tried it and it worked all fine out. But when i got to the Jump the problem is that he Jumps but instead of going slowly up it goes so fast that it seams like its teleporting. And i searched on the web but yeah then i ended here with the hope that someone can help me out cause i still haven´t figgerd it out.(I put the Code in the bottom). If you need any more informations ... to help please let me know and thanks to all that try to help me :)

    Code (CSharp):
    1. using System;
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    6. public class PlayerMovement : MonoBehaviour
    7. {
    8.     private Vector3 playerVelocity;
    9.     private Vector3 move = new Vector3(0, 0, 0);
    12.     private Animator ani;
    13.     private CharacterController ch;
    14.     public Vector3 moveInput;
    15.     private float aniMoveVelocity;
    16.     private float MoveVelocity;
    17.     private bool isJumping;
    19.     [Header("References")]
    20.     [SerializeField]private PlayerInput inputcontroller;
    22.     [Header("Gravity-Settings")]
    23.     [SerializeField]private float groundedGravity = -.05f;
    24.     [SerializeField]private float Gravity = -9.8f;
    26.     [Header("Jumping-Settings")]
    27.     [SerializeField]private float initialJumpVelocity = 3;
    28.     [SerializeField]private float maxJumpHeight = 1;
    29.     [SerializeField]private float maxJumpTime = 0.5f;
    32.     [Header("Movement-Settings")]
    33.     [SerializeField]private float movementSpeed = 3;
    34.     [SerializeField]private float sprintSpeed = 8.5f;
    35.     [SerializeField]private float walkAnimationBlendTime = 1.5f;
    36.     [SerializeField]private float sprintAnimationBlendTime = 1.0f;
    37.     [SerializeField]private float rotationPerFrame = 1.0f;
    42.     void Awake()
    43.     {
    44.         ch = GetComponent<CharacterController>();
    45.         ani = GetComponent<Animator>();
    46.         setupJumpVariables();
    47.     }
    49.     private void FixedUpdate() {
    50.         moveInput = inputcontroller.MovementInput;
    51.         handleAnimations();
    52.         Movement();
    53.         handleGravity();
    54.         jump();
    55.         ch.Move(moveInput * Time.deltaTime);
    56.     }
    58.     private void Update()
    59.     {
    60.     }
    62.     private void handleAnimations()
    63.     {
    64.         ani.SetFloat("MoveVelocity", MoveVelocity);
    65.         aniMoveVelocity = ani.GetFloat("MoveVelocity");
    66.         if(aniMoveVelocity > 0 && inputcontroller.pressMoving == false)
    67.         {
    68.             MoveVelocity = MoveVelocity + walkAnimationBlendTime * -Time.deltaTime;
    69.         }
    70.         if(aniMoveVelocity > 0.53 && inputcontroller.pressSprinting == false)
    71.         {
    72.             MoveVelocity = MoveVelocity + sprintAnimationBlendTime * -Time.deltaTime;
    73.         }
    74.         if(inputcontroller.pressMoving == true && aniMoveVelocity <= 0.5f)
    75.         {
    76.             MoveVelocity = MoveVelocity + walkAnimationBlendTime * Time.deltaTime;
    77.         }
    78.         if(inputcontroller.pressMoving == true && inputcontroller.pressSprinting == true && aniMoveVelocity <= 1f)
    79.         {
    80.             MoveVelocity = MoveVelocity + sprintAnimationBlendTime * Time.deltaTime;
    81.         }
    82.     }
    84.     private void handleGravity()
    85.     {
    86.         if(ch.isGrounded)
    87.         {
    88.             moveInput.y = 0;
    89.         }
    91.         if(!ch.isGrounded)
    92.         {
    93.             moveInput.y = Gravity;  
    94.         }
    95.     }
    98.     private void setupJumpVariables()
    99.     {
    100.         float timeToApex = maxJumpHeight / 2;
    101.         Gravity = (-2 * maxJumpHeight) / Mathf.Pow(timeToApex, 2);
    102.         initialJumpVelocity = (2 * maxJumpHeight) / timeToApex;
    103.     }
    105.     private void jump()
    106.     {
    107.         if(inputcontroller.pressJump && ch.isGrounded && !isJumping)
    108.         {
    109.             print("pressed");
    110.             isJumping = true;
    111.             moveInput.y = initialJumpVelocity;
    112.         }
    113.         if(isJumping && !inputcontroller.pressJump && ch.isGrounded)
    114.         {
    115.             isJumping = false;
    116.         }
    117.     }
    119.     private void Movement()
    120.     {
    121.         if(inputcontroller.pressMoving && !inputcontroller.pressSprinting)
    122.         {
    123.             move.x = moveInput.x * movementSpeed;
    124.             move.y = moveInput.x * movementSpeed;
    125.         }
    126.         if(inputcontroller.pressMoving && inputcontroller.pressSprinting)
    127.         {
    128.             move.x = moveInput.x * sprintSpeed;
    129.             move.y = moveInput.x * sprintSpeed;
    130.         }
    131.     }
    133. }
  2. Kurt-Dekker


    Mar 16, 2013
    Line 124 and line 129 above don't seem correct. You're mixing your x and your y.

    If that's not it, welcome to debugging!

    You must find a way to get the information you need in order to reason about what the problem is.

    Once you understand what the problem is, you may begin to reason about a solution to the problem.

    What is often happening in these cases is one of the following:

    - the code you think is executing is not actually executing at all
    - the code is executing far EARLIER or LATER than you think
    - the code is executing far LESS OFTEN than you think
    - the code is executing far MORE OFTEN than you think
    - the code is executing on another GameObject than you think it is
    - you're getting an error or warning and you haven't noticed it in the console window

    To help gain more insight into your problem, I recommend liberally sprinkling
    statements through your code to display information in realtime.

    Doing this should help you answer these types of questions:

    - is this code even running? which parts are running? how often does it run? what order does it run in?
    - what are the values of the variables involved? Are they initialized? Are the values reasonable?
    - are you meeting ALL the requirements to receive callbacks such as triggers / colliders (review the documentation)

    Knowing this information will help you reason about the behavior you are seeing.

    You can also supply a second argument to Debug.Log() and when you click the message, it will highlight the object in scene, such as

    If your problem would benefit from in-scene or in-game visualization, Debug.DrawRay() or Debug.DrawLine() can help you visualize things like rays (used in raycasting) or distances.

    You can also call Debug.Break() to pause the Editor when certain interesting pieces of code run, and then study the scene manually, looking for all the parts, where they are, what scripts are on them, etc.

    You can also call GameObject.CreatePrimitive() to emplace debug-marker-ish objects in the scene at runtime.

    You could also just display various important quantities in UI Text elements to watch them change as you play the game.

    If you are running a mobile device you can also view the console output. Google for how on your particular mobile target, such as this answer or iOS: or this answer for Android:

    Another useful approach is to temporarily strip out everything besides what is necessary to prove your issue. This can simplify and isolate compounding effects of other items in your scene or prefab.

    Here's an example of putting in a laser-focused Debug.Log() and how that can save you a TON of time wallowing around speculating what might be going wrong:

    When in doubt, print it out!(tm)

    Note: the
    function is an alias for Debug.Log() provided by the MonoBehaviour class.
    Oddness likes this.
  3. Oddness


    Jul 12, 2019
    Something that might help with a lot of small things like this would be DOTween on the asset store. There's a free version of it, and it works very well.

    One cautionary note though -- Tween assets are really easy to use once you figure them out. In fact, they're a little bit too easy and, in my opinion anyway, it's better to be sure that you already know how to do this on your own before you start using short-cuts, otherwise you're cheating yourself out of getting better at coding. If that doesn't matter to you though, or if you're comfortable with where you're at, assets like DOTween, LeanTween, etc. make a lot of little operations like this one much quicker and easier to implement.

    They essentially give you a pre-built tool-kit of static classes and components, which you would otherwise be implementing on your own, and do things like 'move x object y direction for t seconds,' or 'reduce alpha of x object to y value over t seconds'; little operations like that.

    Again though, learning to write C# is a process. If you've never had to bang your head, trying to figure out how to make text print across the screen, in just the way you want it to, and go straight to someone else's implementation of it instead, it seems to me that you're only hurting yourself.

    Asset Store Link:

    With that being said, everything we do is pretty much implementing someone else's shortcuts, assuming we're writing in C#. It's just a matter of whether you want to be reliant on those shortcuts or not.
    Last edited: Nov 24, 2022