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

Whats Wrong With My Script? Character Flys Upwards After Jump Command.

Discussion in 'Scripting' started by GamerWolfOps, Feb 28, 2018.

  1. GamerWolfOps

    GamerWolfOps

    Joined:
    Jan 20, 2015
    Posts:
    14
    Hey i am doing a simple platform game for the mobile phones and i had ran into some problems.

    For some reason my character just fly's or jump's upwards after the " GetMouseButtonDown" jump command.
    In the game preview the character should slowly jump just once and back onto the platform.
    For the character i had also turned on Is Kinematic. Otherwise the character just keeps on falling down.


    Here's my Script.

    Code (CSharp):
    1.  
    2. public class PlayerControls : MonoBehaviour {
    3.     public Rigidbody2D rb;
    4.  
    5.     // Use this for initialization
    6.     void Start () {
    7.    
    8.  
    9.         rb = GetComponent<Rigidbody2D>();
    10.  
    11.     }
    12.    
    13.     // Update is called once per frame
    14.     void Update () {
    15.    
    16.         rb.velocity = new Vector2 (1, rb.velocity.y);
    17.  
    18.         if (Input.GetMouseButtonDown (0)) {
    19.             rb.velocity = new Vector2 (rb.velocity.x, 1);
    20.         }
    21.     }
    22.  
    23. }
    I am not sure what i am missing for the code to work correctly?
    looking forward for some replies.
     
  2. GamerWolfOps

    GamerWolfOps

    Joined:
    Jan 20, 2015
    Posts:
    14
    Sorry about the image source it seems it doesn't work. Looking forward for some answers on the script.
     
  3. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    If you are just beginning, start here: https://unity3d.com/learn/tutorials

    Since you set your character to kinematic, and then set its velocity, it is not affected by gravity. You would have to make it non-kinematic, or apply your own gravity.
     
  4. SparrowGS

    SparrowGS

    Joined:
    Apr 6, 2017
    Posts:
    2,536
    Also you're doing physics inside Update rather then(or than?) in FixedUpdate.

    Check for the input in the update loop so you catch every click and use a flag to trigger the physics.
     
  5. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,140
    Also note you don't have to host your screenshot somewhere, you can add it directly to the forum.
     
  6. GamerWolfOps

    GamerWolfOps

    Joined:
    Jan 20, 2015
    Posts:
    14
    Thank you and thank u for the replies when it comes to scripting i am not new. But this is what i am working on.

    And no the character shouldn't be jumping off straight like that.
     

    Attached Files:

  7. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    I'm pretty sure it's totally okay to change velocity in Update().

    OP: Did my response not help you to resolve your issue?
     
  8. GamerWolfOps

    GamerWolfOps

    Joined:
    Jan 20, 2015
    Posts:
    14
    This new Unity update is sure giving me a headache lol. But i will test it out today thank you.