Search Unity

Need help moving the player while jumping

Discussion in 'Scripting' started by theFeez, Jan 26, 2015.

  1. theFeez

    theFeez

    Joined:
    Jan 26, 2015
    Posts:
    1
    I've been having trouble controlling the player while he is in the air after jumping. With the following code, the player jumps a very small amount in the air and comes back down.

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class basicMovement : MonoBehaviour
    5. {
    6.     public float speed;
    7.  
    8.  
    9.     public float jumpSpeed = 8.0F;
    10.     public float gravity = 10.0F;
    11.  
    12.     private Vector3 currentPos;
    13.     private Transform transform1;
    14.     private float vertVel;
    15.     private Quaternion currentDirection;
    16.     private Vector3 moveDirection = Vector3.zero;
    17.     private CharacterController controller;
    18.  
    19.     void FixedUpdate()
    20.     {
    21.         float h = Input.GetAxis ("Horizontal");
    22.         float v = Input.GetAxis ("Vertical");
    23.         controller = GetComponent<CharacterController> ();
    24.         transform1 = GetComponent<Transform> ();
    25.  
    26.         currentPos = transform.position;
    27.         if(controller.isGrounded)
    28.         {
    29.  
    30.             MovementManagement (h, v);
    31.             moveDirection = new Vector3(Input.GetAxis("Horizontal"), 0f, Input.GetAxis("Vertical"));
    32.        
    33.             moveDirection *= speed;
    34.             if (Input.GetButton("Jump"))
    35.             {
    36.                 moveDirection = new Vector3(Input.GetAxis("Horizontal"), jumpSpeed, Input.GetAxis("Vertical"));
    37.             }
    38.        
    39.         }
    40.         else
    41.         {
    42.             moveDirection = new Vector3(Input.GetAxis("Horizontal"), currentPos.y, Input.GetAxis("Vertical"));
    43.         }
    44.  
    45.  
    46.          
    47.         moveDirection.y -= gravity * Time.deltaTime;
    48.         controller.Move(moveDirection * Time.deltaTime);
    49.        
    50.         currentDirection = transform.rotation;
    51.     }
    52.  
    53.  
    54.  
    55.  
    56.  
    57.     void Rotating(float horizontal, float vertical)
    58.     {
    59.         if(horizontal == 0f && vertical == 0f)
    60.         {
    61.             transform.rotation = currentDirection;
    62.         }
    63.         else
    64.         {
    65.             Vector3 targetDirection = new Vector3 (horizontal, 0f, vertical);
    66.             Quaternion targetRotation = Quaternion.LookRotation (targetDirection, Vector3.up);
    67.            
    68.             rigidbody.rotation = targetRotation;
    69.         }
    70.     }
    71.  
    72.     void MovementManagement(float horizontal, float vertical)
    73.     {
    74.         Rotating(horizontal,vertical);
    75.     }
    76. }

    By taking out the else statement in the FixedUpdate() function, the player jumps the correct height, however I can't move the player until he reaches the ground again. What would I need to do to the code in order to have the player accept input while jumping?
     
  2. B19g_A5s_B411s

    B19g_A5s_B411s

    Joined:
    Jul 27, 2014
    Posts:
    11
    I can't answer your question, but some advice: Move "controller = GetComponent<CharacterController> ()" and "GetComponent<Transform> ()" to VOID START () for optimization.
     
  3. Antistone

    Antistone

    Joined:
    Feb 22, 2014
    Posts:
    2,836
    Line 42 appears to be setting the Y component of your current velocity to the Y component of your current position.