Search Unity

Jumping using addForce();

Discussion in 'Physics' started by GamesWerfer, Aug 27, 2019.

  1. GamesWerfer

    GamesWerfer

    Joined:
    Aug 3, 2019
    Posts:
    10
    So after learning beginner scripting in Unity, I tried making my own game which would be a box platform game in which the player (who would be controlling a box) would have to move through obstacles in order to reach the finish line. One of the abilities that I'm making for the player is a jump feature. At first, I coded a simple movement system in which the player could only move left and right and jump.

    the initial script was something like this

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class PlayerMovement : MonoBehaviour
    6. {
    7.     public Rigidbody rb;
    8.  
    9.     public float force;
    10.     public float jumpForce;
    11.  
    12.     //float doubleJumpCount = 0;
    13.  
    14.     bool doubleJump;
    15.  
    16.     bool jump;
    17.     bool left;
    18.     bool right;
    19.  
    20.  
    21.     // Update is called once per frame
    22.     void Update()
    23.     {
    24.         jump = Input.GetButtonDown("Up");
    25.         left = Input.GetButton("Left");
    26.         right = Input.GetButton("Right");
    27.  
    28.  
    29.  
    30.         /*
    31.         if(jump)
    32.         {
    33.             doubleJumpCount++;
    34.  
    35.             Debug.Log("doubleJumpCount = " + doubleJumpCount);
    36.             if(doubleJumpCount == 3)
    37.             {
    38.                 jump = false;
    39.                 doubleJumpCount = 0;
    40.             }
    41.         }*/
    42.     }
    43.  
    44.     void FixedUpdate()
    45.     {
    46.         if(right)
    47.         {
    48.             rb.AddForce(Vector3.right * force * Time.deltaTime);
    49.         }
    50.         if(left)
    51.         {
    52.             rb.AddForce(Vector3.left * force * Time.deltaTime);
    53.         }
    54.         if(jump)
    55.         {
    56.             rb.AddForce(Vector3.up * jumpForce * Time.deltaTime);
    57.         }
    58.     }
    59. }
    Upon testing, I found out that I could just spam the w-key (I set 'w' as the key for "Up" in the project settings) and the player could make the box flying in the air. Additionally, the box could fall over the platforms in the level because of the physics. To avoid both of these problems, I decided to limit the player to double jumping only and allow the player to move in the forward and backward direction too. I added a few lines of code into my script and this is what I have now:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class PlayerMovement : MonoBehaviour
    6. {
    7.     public Rigidbody rb;
    8.  
    9.     public float force;
    10.     public float jumpForce;
    11.  
    12.     float doubleJumpCount = 0;
    13.  
    14.     bool doubleJump;
    15.  
    16.     bool jump;
    17.     bool forward;
    18.     bool backwards;
    19.     bool left;
    20.     bool right;
    21.  
    22.     private void OnCollisionEnter(Collision collision)
    23.     {
    24.         if (collision.collider.name == "Floor")
    25.         {
    26.             doubleJumpCount = 0;
    27.         }
    28.     }
    29.  
    30.     // Update is called once per frame
    31.     void Update()
    32.     {
    33.         jump = Input.GetButtonDown("Jump");
    34.         left = Input.GetButton("Left");
    35.         right = Input.GetButton("Right");
    36.         forward = Input.GetButton("Up");
    37.         backwards = Input.GetButton("Down");
    38.  
    39.  
    40.  
    41.  
    42.  
    43.         if (jump)
    44.         {
    45.             doubleJumpCount++;
    46.  
    47.             Debug.Log("doubleJumpCount = " + doubleJumpCount);
    48.             if (doubleJumpCount > 2) jump = false;
    49.  
    50.         }
    51.     }
    52.  
    53.     void FixedUpdate()
    54.     {
    55.         if (right) rb.AddForce(Vector3.right * force * Time.deltaTime);
    56.         if (left) rb.AddForce(Vector3.left * force * Time.deltaTime);
    57.         if (forward) rb.AddForce(Vector3.forward * force * Time.deltaTime);
    58.         if (backwards) rb.AddForce(Vector3.back * force * Time.deltaTime);
    59.         if (jump)
    60.         {
    61.             rb.AddForce(Vector3.up * jumpForce * Time.deltaTime);
    62.         }
    63.     }
    64. }
    65.  
    The double jump mechanic works. However, if the box is stationary and I press the jump button only (in this case I set it as "space"), the box would jump to the direction of the positive or negative axis of x. Sometimes, after double jumping once, it would just slide towards the negative axis of x whenever I press space again. The effect I wanted it to produce is to move vertically up towards the positive side of the y-axis only. What could be the problem here?
     
    Last edited: Aug 27, 2019
  2. GamesWerfer

    GamesWerfer

    Joined:
    Aug 3, 2019
    Posts:
    10
    I should also note that I tried changing the if(jump) conditional in the FixedUpdate() function into these two versions:


    Code (CSharp):
    1. if(jump)
    2.         {
    3.             rb.AddForce(new Vector3(0,1,0) * jumpForce * Time.deltaTime);
    4.         }
    and

    Code (CSharp):
    1. if(jump)
    2.         {
    3.             rb.AddForce(new Vector3(1,1,0) * jumpForce * Time.deltaTime);
    4.         }
    the values I have set in the script component are:
    Rb: Player (Rigid Body)
    Force: 1000
    Jump Force: 10000


    Also, I inserted a physics material to my platforms which is set to 0 static friction and 0 dynamic friction