Search Unity

Unity Roll A ball Tutorial Jump Lag.

Discussion in 'Community Learning & Teaching' started by graemecurry, Sep 17, 2017.

  1. graemecurry

    graemecurry

    Joined:
    Sep 17, 2017
    Posts:
    1
    So, I've just started learning the unity engine and wanted to expand the first project a little to in-cooperate jumping. From guesswork and referring to the documentation i managed to come to this solution. However i noticed the ball would frequently ignore requests to jump. How should i change this such that The player can jump onto objects within a 3D world without any 'Input lag'
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5.  
    6. public class PlayerControl : MonoBehaviour {
    7.    
    8.     public float speed;
    9.     public Text countText;
    10.     public Text winText;
    11.     public int CollectableNum;
    12.     public float jumpForce;
    13.  
    14.     private Rigidbody rb;
    15.     private int count;
    16.     private bool hasJumped = false;
    17.     private bool isGrounded = true;
    18.  
    19.     void Start ()
    20.     {
    21.         rb = GetComponent<Rigidbody> ();
    22.         count = 0;
    23.         SetCountText();
    24.         winText.text = "";
    25.     }
    26.     void FixedUpdate()
    27.     {
    28.         float moveHorizontal = Input.GetAxis ("Horizontal");
    29.         float moveVertical = Input.GetAxis ("Vertical");
    30.  
    31.         Vector3 movement = new Vector3 (moveHorizontal, 0.0f, moveVertical);
    32.         rb.AddForce (movement * speed);
    33.  
    34.  
    35.     }
    36.  
    37.     void OnCollisionStay(Collision other)
    38.     {
    39.         if (other.gameObject.CompareTag ("Ground/Object"))
    40.         {
    41.             if (Input.GetKeyDown ("space"))
    42.             {
    43.                 rb.AddForce (0, jumpForce, 0, ForceMode.Impulse);
    44.             }
    45.         }
    46.     }
    47.  
    48.  
    49.     void OnTriggerEnter(Collider other)
    50.     {
    51.         if (other.gameObject.CompareTag ("Pickup"))
    52.         {
    53.             other.gameObject.SetActive (false);
    54.             count = count + 1;
    55.             SetCountText ();
    56.         }
    57.     }
    58.     void SetCountText ()
    59.     {
    60.         countText.text = "Count: " + count.ToString ();
    61.         if (count >= CollectableNum)
    62.         {
    63.             winText.text = "You Win!";
    64.         }
    65.     }
    66. }
    67.