Click here to Skip to main content
15,446,232 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
So i have a script that moves my player plays the animations as needed and jumps.
But there are 2 problems.
1. On idle my characters speed is 0 but where i run the game the speed when i dont move the player goes 0 then 10 then 0 then 10 again, and is this loop of increase and decrease of speed, and the animation for slow run is playing in a buggy way.

2. My jump height is controlled by how fust i am running. I have 3 states of my character,
Idle, SlowRun, FastRun,when i SlowRun then it jumps normal but when i FastRun the jump height is higher.
I will paste my entire code bellow, thank you.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class playermovement : MonoBehaviour
    private CharacterController controller;
    public float playerSpeed = 0;
    public float gravity = 5f;
    public float jumpSpeed = 5f;
    private Vector3 move =;
    private Animator anim;

    private float verticalVelocity;

    private void Start()
        controller = GetComponent<CharacterController>();
        anim = GetComponentInChildren<Animator>();

    void Update()

    private void Move()
        if (controller.isGrounded)
            float horizontal = Input.GetAxis("Horizontal");
            float vertical = Input.GetAxis("Vertical");

            move = new Vector3(horizontal, 0, vertical);
            move = transform.TransformDirection(move);

        if (move ==
        else if (move != && !Input.GetKey(KeyCode.LeftShift))

        else if (move != && Input.GetKey(KeyCode.LeftShift))

        if (Input.GetKeyDown(KeyCode.Space))
            move.y = jumpSpeed;

        move.y -= gravity * Time.deltaTime;
        controller.Move(move * Time.deltaTime * playerSpeed);

    private void Idle()
        playerSpeed = 0;
        anim.SetFloat("Speed", 0, 0.1f, Time.deltaTime);

    private void SlowRun()
        playerSpeed = 10f;
        anim.SetFloat("Speed", 0.5f, 0.1f, Time.deltaTime);

    private void FastRun()
        playerSpeed = 20f;
        anim.SetFloat("Speed", 1, 0.1f, Time.deltaTime);

What I have tried:

I cant figure out why it does this, I have gone online, whatched a lot of videos but i have no idea. I have tried having 2 CharacterController.Move() functions but the it becomes a mess, atleast for my knowledge of c#.
When i say a mess, i mean that then the jump function doesnt work all the time and when i am near a building or steps i get thrown high up like of the map when i press the jump button.
I can not use LayerMask beacuse i need the enviroment to have different layers for other stuff i want to do.
Thank you.

1 solution

1. for any update you call the function Move that change the vetore move based on gravity. It start with 0 then it became different from 0.

2. Jump speed is always constant not related to the playerSpeed.
Share this answer
simple world 8-Mar-22 10:34am    
Yes but how can i fix them? Jump speed is constant but here is connected with my speed and i cant tell why, when i delete the playerspeed variable the player can still move and i have no clue why

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900