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): using System.Collections; using System.Collections.Generic; using UnityEngine; public class PlayerMovement : MonoBehaviour { public Rigidbody rb; public float force; public float jumpForce; //float doubleJumpCount = 0; bool doubleJump; bool jump; bool left; bool right; // Update is called once per frame void Update() { jump = Input.GetButtonDown("Up"); left = Input.GetButton("Left"); right = Input.GetButton("Right"); /* if(jump) { doubleJumpCount++; Debug.Log("doubleJumpCount = " + doubleJumpCount); if(doubleJumpCount == 3) { jump = false; doubleJumpCount = 0; } }*/ } void FixedUpdate() { if(right) { rb.AddForce(Vector3.right * force * Time.deltaTime); } if(left) { rb.AddForce(Vector3.left * force * Time.deltaTime); } if(jump) { rb.AddForce(Vector3.up * jumpForce * Time.deltaTime); } } } 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): using System.Collections; using System.Collections.Generic; using UnityEngine; public class PlayerMovement : MonoBehaviour { public Rigidbody rb; public float force; public float jumpForce; float doubleJumpCount = 0; bool doubleJump; bool jump; bool forward; bool backwards; bool left; bool right; private void OnCollisionEnter(Collision collision) { if (collision.collider.name == "Floor") { doubleJumpCount = 0; } } // Update is called once per frame void Update() { jump = Input.GetButtonDown("Jump"); left = Input.GetButton("Left"); right = Input.GetButton("Right"); forward = Input.GetButton("Up"); backwards = Input.GetButton("Down"); if (jump) { doubleJumpCount++; Debug.Log("doubleJumpCount = " + doubleJumpCount); if (doubleJumpCount > 2) jump = false; } } void FixedUpdate() { if (right) rb.AddForce(Vector3.right * force * Time.deltaTime); if (left) rb.AddForce(Vector3.left * force * Time.deltaTime); if (forward) rb.AddForce(Vector3.forward * force * Time.deltaTime); if (backwards) rb.AddForce(Vector3.back * force * Time.deltaTime); if (jump) { rb.AddForce(Vector3.up * jumpForce * Time.deltaTime); } } } 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?
I should also note that I tried changing the if(jump) conditional in the FixedUpdate() function into these two versions: Code (CSharp): if(jump) { rb.AddForce(new Vector3(0,1,0) * jumpForce * Time.deltaTime); } and Code (CSharp): if(jump) { rb.AddForce(new Vector3(1,1,0) * jumpForce * Time.deltaTime); } 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