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

Resolved Help Needed Cannot Jump 3D Object

Discussion in 'Scripting' started by Highberk, Jul 10, 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!";
    }
    }
    }
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,971
    Highberk likes this.
  3. Highberk

    Highberk

    Joined:
    Jun 25, 2020
    Posts:
    5
    Thank you very much! I reposted as you said.