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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Character controller jump is unresposive

Discussion in 'Scripting' started by Lexo18, Apr 26, 2015.

  1. Lexo18

    Lexo18

    Joined:
    May 12, 2013
    Posts:
    34
    Onto this gameObject is attached this script along with a character controller.
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. public class PlayerMovement : MonoBehaviour {
    4.     public float curSpeed;
    5.     public float acceleration = 5;
    6.     public float decceleration = .2f;
    7.     public float jump = 20;
    8.     public float gravity = 21;
    9.     public float airAcceleration = 2.5f;
    10.     public float jumpAllowTime = .1f;
    11.     float jumpAllowTrack;
    12.     float moveSmooth;
    13.     CharacterController cont;
    14.     bool run = false;
    15.     Vector3 curMove;
    16.  
    17.     float walkMoveV;
    18.     float runMoveV;
    19.     float stopMoveV;
    20.     float airAccelerationV;
    21.     private Stats Player;
    22.  
    23.     void Awake (){
    24.         Player = GetComponent<Stats>();
    25.     }
    26.     // Use this for initialization
    27.     void Start () {
    28.         cont = GetComponent<CharacterController>();
    29.     }
    30.  
    31.     void Update(){
    32.    
    33.         if(Input.GetKey (KeyCode.A) || Input.GetKey (KeyCode.D))
    34.             run = true;
    35.          else
    36.             run = false;
    37.    
    38.    
    39.         curMove = new Vector3(curSpeed, curMove.y, 0);
    40.    
    41.  
    42.         if(run == true && Input.GetKey (KeyCode.LeftShift))
    43.             curSpeed = Mathf.SmoothDamp(curSpeed, Input.GetAxisRaw ("Horizontal") * Player.runSpeed, ref walkMoveV, moveSmooth);
    44.    
    45.         else if (run == true)
    46.             curSpeed = Mathf.SmoothDamp (curSpeed, Input.GetAxisRaw ("Horizontal") * Player.speed, ref runMoveV , moveSmooth);
    47.    
    48.         else
    49.             curSpeed = Mathf.SmoothDamp (curSpeed, 0, ref stopMoveV , moveSmooth);
    50.    
    51.    
    52.         if(Input.GetKeyDown (KeyCode.T) && jumpAllowTrack >= 0)
    53.             curMove.y = jump;
    54.    
    55.         if(cont.isGrounded){
    56.                 curMove.y=0;
    57.                 moveSmooth = acceleration;
    58.                 jumpAllowTrack = jumpAllowTime;
    59.         }
    60.    
    61.         if(!cont.isGrounded){
    62.             curMove -= new Vector3(0, gravity * Time.deltaTime, 0);
    63.             moveSmooth = airAcceleration;
    64.             jumpAllowTrack -= Time.deltaTime;
    65.             }
    66.         cont.Move(curMove*Time.deltaTime);
    67.    
    68.     }
    69. }
    The problem is it will only jump when I hit the t button about 1/3 of the time. How can I make it so that it will jump everytime I hit the t button.
     
  2. hamsterbytedev

    hamsterbytedev

    Joined:
    Dec 9, 2014
    Posts:
    353
    Ignoring the fact that you have no Update function; I assume this was a copy paste error. I'm gonna have to say it has something to do with your jumpAllowTrack variable. Do some debugs on that variable when you are jumping and see what's going on with it.
     
  3. Lexo18

    Lexo18

    Joined:
    May 12, 2013
    Posts:
    34
    I tried removing the variable and just checking to see if the controller was touching the ground but the problem still persists.
     
  4. hamsterbytedev

    hamsterbytedev

    Joined:
    Dec 9, 2014
    Posts:
    353
  5. Lexo18

    Lexo18

    Joined:
    May 12, 2013
    Posts:
    34
    Based on what I debugged it seems like it is only registering when the update function is called and it runs though that line and not in between.
     
  6. hamsterbytedev

    hamsterbytedev

    Joined:
    Dec 9, 2014
    Posts:
    353
    Well, it would only run when the Update function is called because its in the update function.

    curMove -=newVector3(0, gravity*Time.deltaTime, 0);

    Maybe this line is cancelling out the upward movement before it can occur. Try applying your movement before your grounded checks maybe?