Search Unity

Glitchy Jump in beginning Third Person Controller

Discussion in 'Scripting' started by Schribly, Jul 27, 2014.

  1. Schribly

    Schribly

    Joined:
    Mar 26, 2014
    Posts:
    4
    Hey guys!
    I decided to make a really basic third person player controller just to get a better understanding of how everything in unity and c# coding works but I ran into a little problem. The movement and camera rotation work how I would like them to, but the jumping is glitchy. The player will jump up, kind of pause in the air (y coor), then shoot forward in the direction you are holding, and then fall normally. This only happens when you are jumping in a direction. Somehow the jump is effecting the side and forward speed...but I don't know if that is my script or misuse of the engine?

    Either way help is appreciated :) I am normally a java coder and understand a decent amount about how languages work so don't be afraid to get technical.

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class PlayerControl : MonoBehaviour
    5. {
    6.  
    7.     public float mouseSpeed = 2.5f;
    8.     public float upDownRange = 10.0f;
    9.     float vertRot = 0;
    10.  
    11.     public float movementSpeed = 20;
    12.     public float jumpForce = 5.0f;
    13.  
    14.     float currForward;
    15.     float currSide;
    16.  
    17.     float vertSpeed = 0;
    18.  
    19.  
    20.     // Use this for initialization
    21.     void Start ()
    22.     {
    23.        
    24.     }
    25.    
    26.     // Update is called once per frame
    27.     void Update ()
    28.     {
    29.  
    30.         float rotLR = Input.GetAxis ("Mouse X") * mouseSpeed;
    31.         transform.Rotate (0, rotLR, 0);
    32.         vertRot -= Input.GetAxis ("Mouse Y") * mouseSpeed * 0.7f;
    33.         vertRot = Mathf.Clamp (vertRot, -upDownRange, upDownRange);
    34.         Camera.main.transform.localRotation = Quaternion.Euler (vertRot, 0, 0);
    35.  
    36.         // Movement
    37.  
    38.         CharacterController cc = new CharacterController ();
    39.         cc = GetComponent <CharacterController>();
    40.  
    41.         currForward = Input.GetAxis ("Vertical");
    42.         currSide = Input.GetAxis ("Horizontal");
    43.  
    44.         float forwardSpeed = currForward * movementSpeed * Time.deltaTime;
    45.         float sideSpeed = currSide * movementSpeed * Time.deltaTime;
    46.  
    47.         if(cc.isGrounded && Input.GetKeyDown("space"))
    48.         {
    49.             vertSpeed = jumpForce * Time.deltaTime;
    50.         }
    51.         else if(cc.isGrounded)
    52.         {
    53.             vertSpeed = 0;
    54.         }
    55.         else if(!cc.isGrounded)
    56.         {
    57.             vertSpeed += Physics.gravity.y * Time.deltaTime;
    58.         }
    59.  
    60.         Vector3 speed = new Vector3 (sideSpeed, vertSpeed, forwardSpeed);
    61.         speed = transform.rotation * speed;
    62.  
    63.         cc.Move(speed);
    64.     }
    65. }
    66.  
     

    Attached Files:

  2. Sykoo

    Sykoo

    Joined:
    Jul 25, 2014
    Posts:
    1,394
    Okay, I'm not at all good in C# though I'm a JAvascript person, but are you using gravity in your script? And do you use CC (CharacterController) or Rigidbody on the prefab itself?
     
  3. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148