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. Voting for the Unity Awards are OPEN! We’re looking to celebrate creators across games, industry, film, and many more categories. Cast your vote now for all categories
    Dismiss Notice
  3. Dismiss Notice

jump newbie script need help

Discussion in 'Scripting' started by unity_Gh6RsD6Mu8GRfQ, May 31, 2018.

  1. unity_Gh6RsD6Mu8GRfQ

    unity_Gh6RsD6Mu8GRfQ

    Joined:
    May 28, 2018
    Posts:
    6
    sory iam newbie
    everytime i press jump and hit object like cube or something, i got high jump
    whats wrong with my script

    *sory my bad english

    Code (CSharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5.  
    6. public class PlayerMove : MonoBehaviour {
    7.  
    8.     public float walk;
    9.     public float run;
    10.     public float jump;
    11.     public Rigidbody rb;
    12.     public bool isGround;
    13.  
    14.  
    15.     void Start () {
    16.  
    17.         rb = GetComponent<Rigidbody>();
    18.     }
    19.    
    20.    
    21.     void Update () {
    22.  
    23.         float x = Input.GetAxis("Horizontal") * walk * Time.deltaTime;
    24.         float y = Input.GetAxis("Vertical") * walk * Time.deltaTime;
    25.  
    26.         transform.Translate(x, 0, y);
    27.  
    28.         if (Input.GetButton("Jump") && isGround)
    29.         {
    30.             GetComponent<Rigidbody>().AddForce(Vector3.up * jump * Time.deltaTime, ForceMode.Impulse);
    31.             isGround = false;
    32.         }
    33.  
    34.     }
    35.  
    36.     void OnCollisionStay ()
    37.     {
    38.         isGround = true;
    39.     }
    40. }
    41.  
     
  2. Doug_B

    Doug_B

    Joined:
    Jun 4, 2017
    Posts:
    1,596
    You are setting isGround to true in
    void OnCollisionStay()
    . So when you hit any object, PlayerMove now thinks the object is grounded and will accept the jump input again.

    You need to be careful about how you handle isGrounded. For example, one fix might be to check that the object you have hit is a floor before you set isGrounded to true. A potential problem there is if you hit the floor above with your head, jump would again be active and you could jump up again. This may, or may not, make sense for your game.

    Have you tried following the Unity platform tutorial?
     
  3. unity_Gh6RsD6Mu8GRfQ

    unity_Gh6RsD6Mu8GRfQ

    Joined:
    May 28, 2018
    Posts:
    6
    thanks , very helpfull

    i absolutely new in c#, and i made that script basicly from my logic, and i miss that logic about collision

    i will learn more from unity platform tutorial ,
    i just learn from youtube before

    thanks
     
    Doug_B likes this.
  4. Shayke

    Shayke

    Joined:
    Dec 8, 2017
    Posts:
    352
    And about the rigidbody, you already set him on Start so you don't need to get the component each time.
    You can write it that way:
    rb.AddForce(Vector3.up * jump * Time.deltaTime, ForceMode.Impulse);
     
    unity_Gh6RsD6Mu8GRfQ likes this.
  5. unity_Gh6RsD6Mu8GRfQ

    unity_Gh6RsD6Mu8GRfQ

    Joined:
    May 28, 2018
    Posts:
    6
    omg i miss that too
    lol

    thanks for helping me
     
  6. WoodcockCreative

    WoodcockCreative

    Joined:
    Feb 16, 2017
    Posts:
    45
    I'm not sure if this helps, but a while ago, someone asked me to show them how to do a simple jump using animations with a mouse click. You look like you are trying a physics based approach, so this way is completely different. But, the package is still on my website if you want to take a look:

    http://www.woodcockcreative.com/EthanJumping.unitypackage
     
  7. unity_Gh6RsD6Mu8GRfQ

    unity_Gh6RsD6Mu8GRfQ

    Joined:
    May 28, 2018
    Posts:
    6