Search Unity

Unity 5 - jump script issue

Discussion in 'Scripting' started by Legit Tactical, Mar 6, 2015.

  1. Legit Tactical

    Legit Tactical

    Joined:
    Sep 22, 2013
    Posts:
    27
    So Unity 5 brought us a lot of great things but it also managed to make thing somewhat difficult.

    Since they changed their API and removed/ changed some functions. They also switched to PhysX 3.3, if I'm not mistaken, which changes the way physics are hanled in Unity a little bit.

    I used this code for jumping for my project in Unity 4.6.3 and I'm trying to port my project over to Unity 5. But, when I use this code it either doesn't work or doesn't work as intended.

    Code (CSharp):
    1. private float distToGround;
    2.  
    3.     bool isGrounded()
    4.     {
    5.         return Physics.Raycast(transform.position, -Vector3.up, distToGround = 0.1f);
    6.     }
    7.  
    8.  
    9. void FixedUpdate()
    10. {
    11.         if (Input.GetButtonDown("Jump") && isGrounded());
    12.         {
    13.             playerRigidBody.AddForce(transform.position * jumpForce * Time.deltaTime);
    14.         }
    15.  
    16. }
    I'm not the best coder by any means but I am quite proficient and I know for a fact that this used to work and it should still work.

    Am I missing something here?
     
  2. Legit Tactical

    Legit Tactical

    Joined:
    Sep 22, 2013
    Posts:
    27
    My main problem is that when I play the game. My character flies into infinity and I get this error.

    Screen position out of view frustum (screen pos 0.000000, 474.000000, 1000.000000) (Camera rect 0 0 431 474)


    or the character just doesn't jump.
     
  3. nygren

    nygren

    Joined:
    Feb 13, 2014
    Posts:
    33
    Could it be that your character has a Collider and your Physics.Raycast hits it?
     
  4. sheiro

    sheiro

    Joined:
    Mar 6, 2015
    Posts:
    7
    Hi,
    i´m a noob in coding but i doing something similar.
    When i upgraded to Unity5 today, my old code was changed slightly.
    Know there is the "GetComponent" which wasn´t there before i upgraded:

    Code (JavaScript):
    1.     if (Input.GetButtonDown("Jump") && isGrounded)
    2.     {
    3.         Jumping = true;
    4.        
    5.         jumpDirection = moveDirection;
    6.        
    7.         GetComponent.<Rigidbody>().AddForce((transform.up) * jumpSpeed);
    8.     }    
    Hope this helps a little.
     
  5. Legit Tactical

    Legit Tactical

    Joined:
    Sep 22, 2013
    Posts:
    27
    That could be the issue. I'll check.
     
  6. Legit Tactical

    Legit Tactical

    Joined:
    Sep 22, 2013
    Posts:
    27
    I got it to work.

    I just had to use velocity instead of trying to AddForce to it.

    Code (CSharp):
    1.  if (Input.GetButtonDown("Jump") && isGrounded())
    2.         {
    3.             playerBody.velocity = new Vector3(0.0f, jumpForce, 0.0f);
    4.         }

    Thanks for your help, guys.
     
unityunity