Search Unity

  1. Good news ✨ We have more Unite Now videos available for you to watch on-demand! Come check them out and ask our experts any questions!
    Dismiss Notice

Unable to Jump C#

Discussion in 'Getting Started' started by Highberk, Jul 9, 2020.

  1. Highberk

    Highberk

    Joined:
    Jun 25, 2020
    Posts:
    5
    Hi everyone, as you can guess im Beginner to coding and Unity. I merged few codes but I couldnt figure out how to solve jumping. When I change Y value its keeps moving constantly according to jump speed. Can some one give me a hand?

    XXXXXXXXXXXXXXXXXXXXXXXX


    using UnityEngine;
    using UnityEngine.UI;
    using System.Collections;

    public class CC : MonoBehaviour
    {

    public float speed;
    public Text countText;
    public Text winText;
    //
    public Vector3 jump;
    public float jumpForce = 2.0f;

    public bool isGrounded;
    //
    private Rigidbody rb;
    private int count;

    void Start()
    {
    rb = GetComponent<Rigidbody>();
    count = 0;
    SetCountText();
    winText.text = "";
    //
    jump = new Vector3(0.0f, 2.0f, 0.0f);
    //
    }
    void OnCollisionStay()
    {
    isGrounded = true;
    }



    void FixedUpdate()
    {
    float moveHorizontal = Input.GetAxis("Horizontal");
    float moveVertical = Input.GetAxis("Vertical");
    Vector3 movement = new Vector3(moveHorizontal, 0.0f, moveVertical);
    rb.AddForce(movement * speed);
    //
    {
    if (Input.GetKeyDown(KeyCode.Space) && isGrounded)
    {
    if (transform.position.y <= 1.05f)
    {
    rb.AddForce(jump * jumpForce, ForceMode.Impulse);
    isGrounded = false;
    }
    }
    }
    //
    }
    void OnTriggerEnter(Collider other)
    {
    if (other.gameObject.CompareTag("Capsule"))
    {
    other.gameObject.SetActive(false);
    count = count + 1;
    SetCountText();
    }
    }
    void SetCountText()
    {
    countText.text = "Count: " + count.ToString();
    if (count >= 3)
    {
    winText.text = "You Win!";
    }
    }
    }
     
unityunity