Search Unity

Noob questions

Discussion in 'Scripting' started by RoiDanielsen, Jan 27, 2012.

  1. RoiDanielsen

    RoiDanielsen

    Joined:
    Nov 10, 2010
    Posts:
    130
    This is what I have so far. Now I want to make him jump, but if I do it the same way I did the movement, how do I make him fall back to the ground? I saw someone talking about raycasting or something, can someone explain me how I could achieve to make him jump?

    Thanks for your time!
     
    Last edited: Jan 27, 2012
  2. TheCasual

    TheCasual

    Joined:
    Sep 30, 2010
    Posts:
    1,286
    GetKey , not GetKeyDown

    That should work out for you.
     
  3. RoiDanielsen

    RoiDanielsen

    Joined:
    Nov 10, 2010
    Posts:
    130
    Bump, got a new question.
     
  4. snortop

    snortop

    Joined:
    Dec 16, 2011
    Posts:
    194
    You really should just start learning unity3D before jumping into something.
    Maybe learn a few thing first.

    I can recomend Walkerboys Tutorial the is a good Maria turorial there.

    But you check for button "jump" is press then move the character in the y direction. (If you viewing from side) like a 2D scrolling game.

    Code (csharp):
    1.  
    2. var JumpSpeed : int = 10;
    3. if (Input.GetKeyDown("jump")) {
    4.     transform.translate(0,JumpSpeed,0);
    5. }
    6.  
    Also check out move and simpelmove.
     
  5. RoiDanielsen

    RoiDanielsen

    Joined:
    Nov 10, 2010
    Posts:
    130
    I know Unity pretty well, but I'm just completely horrible at coding, and I dont understand all of the examples in the scripting reference :(

    I tried doing exactly what you described, but that wont work because my character wont fall down to the ground again. He just keeps going up X amount of units and stays there.
     
  6. Yannic

    Yannic

    Joined:
    Jan 7, 2012
    Posts:
    27
    Use instead a Rigidbody and move it with Rigidbody.AddRelativeForce() ;)
     
  7. snortop

    snortop

    Joined:
    Dec 16, 2011
    Posts:
    194
    Then the code works ^^

    you can keep jumping because the is no check for grounding.
    If you have a Character controller on your mesh you can check for grounding.
    So as long as player not grounded he shouldent be able to push jump.
    And if not grounded move down.

    so would be something like.

    Code (csharp):
    1.  
    2. var JumpSpeed : int = 10;
    3. var gravity : int : 10;
    4.  
    5.  
    6. var controller = GetComponent(CharacterController);
    7.  
    8. if (controller.isGrounded) {
    9.     if (Input.GetKeyDown("jump")) {
    10.         transform.translate.y += jumpSpeed;
    11.     }
    12. }
    13.  
    14. // Apply gravity
    15. transform.translate.y -= gravity * Time.deltaTime;
    16.    
    17.  
    But this will also have it move tru the ground as the is no stopping translate.
    So the is another way, using move and simpelmove.

    And check move. Here the is also a script about all this :). That why i said so in last post.

    But that script work perfectly if you are in a 3D world.

    So if it 2D just remove vertical check.
     
  8. Yannic

    Yannic

    Joined:
    Jan 7, 2012
    Posts:
    27
  9. snortop

    snortop

    Joined:
    Dec 16, 2011
    Posts:
    194
    In what way is it slower?
     
    Last edited: Jan 28, 2012
  10. Yannic

    Yannic

    Joined:
    Jan 7, 2012
    Posts:
    27
    Its not good for the performance.