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

Can't make 2d object jump using unity's code

Discussion in 'Scripting' started by Zohresh, Apr 12, 2014.

  1. Zohresh

    Zohresh

    Joined:
    Apr 12, 2014
    Posts:
    7
    The object floats up as I hold spacebar, then floats back down. We are using rigided2D box Collides.


    Here is the code.

    using UnityEngine;
    using System.Collections;

    public class Example : MonoBehaviour {
    public float speed = 6.0F;
    public float jumpSpeed = 8.0F;
    public float gravity = 20.0F;
    private Vector3 moveDirection = Vector3.zero;
    void Update() {
    CharacterController controller = GetComponent<CharacterController>();
    if (controller.isGrounded) {
    moveDirection = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
    moveDirection = transform.TransformDirection(moveDirection);
    moveDirection *= speed;
    if (Input.GetButton("Jump"))
    moveDirection.y = jumpSpeed;

    }
    moveDirection.y -= gravity * Time.deltaTime;
    controller.Move(moveDirection * Time.deltaTime);
    }
    }
     
  2. lorenalexm

    lorenalexm

    Joined:
    Dec 14, 2012
    Posts:
    307
    Since you're using Input.GetButton, it is constantly polling for that button. You might be looking for Input.GetButtonDown or Input.GetButtonUp
     
  3. Zohresh

    Zohresh

    Joined:
    Apr 12, 2014
    Posts:
    7
    Didn't change anything :(
     
  4. Zohresh

    Zohresh

    Joined:
    Apr 12, 2014
    Posts:
    7
    Can't seem to get this to work, is there another way I could write this code.
     
  5. Zohresh

    Zohresh

    Joined:
    Apr 12, 2014
    Posts:
    7
  6. Shinobi1507

    Shinobi1507

    Joined:
    Sep 8, 2010
    Posts:
    220
    Not sure if this will help... but here is some old code that I used while prototyping my latest project...
    I looked through your code and it seemed a bit off, I didn't have a whole lot of time haha. Sorry. I'll take a second look in a bit. but I had this project open so I figured I could post some old code.


    Code (csharp):
    1.  
    2.             //Gives that initial push, all handled through the rigidbody2D.
    3.             //Jump being an adjustable float.
    4.             //Jump mod tells the game to start running the code that adjusts height of jump.
    5.             if(Input.GetButtonDown("Jump"))
    6.             {
    7.                 if(isGrounded)
    8.                 {
    9.                     this.rigidbody2D.velocity += new Vector2(0,Jump);
    10.                     isGrounded = false;
    11.                     JumpModActive = true;
    12.                 }
    13.             }
    14.            
    15.             //While the button is held, this adds more velocity to the jump... Making the jump height controlled by player.
    16.             if(JumpModActive)
    17.             {
    18.                 JumpModtime += Time.deltaTime;
    19.                 if(JumpModtime >= JumpModTime)
    20.                 {
    21.                     JumpModActive = false;
    22.                     JumpModtime = 0;
    23.                 }
    24.                 this.rigidbody2D.AddForce(new Vector2(0,JumpModifier * Time.deltaTime));
    25.             }
    26.            
    27.             //This controls how high the jump is. Based on how long the player holds jump down.
    28.             if(Input.GetKeyUp(KeyCode.Space) || Input.GetKeyUp(KeyCode.UpArrow))
    29.             {
    30.                 JumpModActive = false;
    31.             }
    32.  
    33.  

    Here is how I got figured if it was grounded.... Not the best way. You could do a few different things to get this working better. Like having colliders on the feet of the character trigger this. So it KNEW it was on the ground.

    Code (csharp):
    1.  
    2.     void OnCollisionEnter2D()
    3.     {
    4.         isGrounded = true;
    5.     }
    6.  
     
  7. Zohresh

    Zohresh

    Joined:
    Apr 12, 2014
    Posts:
    7
    I still get the same results with your code, sorry for the late response ended up in the hospital with blood poisoning...
     
  8. Zohresh

    Zohresh

    Joined:
    Apr 12, 2014
    Posts:
    7
    Bump, I can not figure this out. My object just keeps floating up when I hold the key. Then it floats back down...
     
  9. Shinobi1507

    Shinobi1507

    Joined:
    Sep 8, 2010
    Posts:
    220
    Hmmm, my code if not setup properly will give that effect. However its setup so the jump height is modified by how long you hold down the jump button. If you want you can rip that section out and try it with just the most basic jump script. Not sure if you want jump height to be modified but it will help narrow down the problem.

    Just rip out the "if(JumpModActive)" section of my code.

    Sorry for the late response as two of the servers in my rack failed so I spent much of the last two days getting hardware replacements and getting them back online. Also, I hope you're doing better after being in the hospital.