I am trying to make my 2d character jump, and am using AddForce to do so. Whenever I jump however, the character accelerates way too fast, and then falls to the ground at normal speed, even though gravity is handled by a nearly exactly similar add force (Yes, i know that rigid bodies have gravity built in, but this creates the same exact problem).
I have tried multiple different things, such as changing the velocity, instead of adding force, decreasing and raising the force, and every thing I’ve tried I still have the same result. Here is my code:
using UnityEngine;
using System.Collections;
public class playerController : MonoBehaviour {
[SerializeField]
private float speed;
[SerializeField]
private float jumpHeight;
[SerializeField]
Transform[] groundPoints;
[SerializeField]
private LayerMask groundLayer;
[SerializeField]
private float overlapRadius;
private bool grounded;
[SerializeField]
Rigidbody2D rb;
void Start()
{
}
void FixedUpdate()
{
movement();
grounded = isGrounded();
}
void movement()
{
float yMove = jumpHeight;
if (grounded & Input.GetButtonDown("Jump"))
{
rb.AddForce(new Vector2(0, yMove));
Debug.Log(yMove);
}
rb.AddForce(Physics2D.gravity*10);
float xMove = Input.GetAxis("Horizontal") * speed;
rb.velocity = new Vector2(xMove, 0);
}
bool isGrounded()
{
foreach (Transform point in groundPoints)
{
Collider2D[] colliders = Physics2D.OverlapCircleAll(point.position, overlapRadius, groundLayer);
for (int i = 0; i < colliders.Length; i++)
{
if (colliders*.gameObject != gameObject)*
I can’t help a lot but the way you made the jump is making it goes high wildly you can instead use
add force to the rigid body with a new vector2 o 3 varribale
//this function able my player to jump by adding force to his y axis
void Jump(){
rb2d.AddForce (new Vector2 (0,jumpForce));
}
this a preview from my jump code the jump force has been set in the inspector to 950f but you can play with it till you get the effect you liked , it deppendes on your game
Sorry for my english i can’t write that good thank
this is my game movement script i re used all the time
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerCtrl : MonoBehaviour {
public float maxSpeed;
public float jumpForce;
public bool isGrounded;
public bool isFacingRight = true;
public bool canMovWhileJumping;
public Transform groundCheck;
public LayerMask whatisGround;
public float checkRadius = 0.2f;
private Rigidbody2D rb2d;
private Animator anim;
void Awake () {
rb2d = GetComponent<Rigidbody2D> ();
anim = GetComponent<Animator> ();
}
//this function for physical stuff and calculation
void FixedUpdate () {
checkForGround ();
Move ();
}
void Update(){
anim.SetBool ("Grounded", isGrounded);
if (Input.GetKeyDown (KeyCode.Space) && isGrounded) {
Jump ();
}
}
//this function moves the player
void Move(){
//this if stat stop the player from moving while he's jumping
if (!isGrounded && !canMovWhileJumping)
return;
//here i used get axis to get a value between 0 and 1 when the player hit the right and left arrows
float move = Input.GetAxis ("Horizontal");
anim.SetFloat ("Speed", Mathf.Abs (move));
//here my players move by effecting his rigidbody compement velocity
rb2d.velocity = new Vector2 (move * maxSpeed, rb2d.velocity.y);
if (move < 0 && isFacingRight)
Flip ();
if (move > 0 && !isFacingRight)
Flip ();
}
//this function able my player to jump by adding force to his y axis
void Jump(){
rb2d.AddForce (new Vector2 (0,jumpForce));
}
//this function check if there is a ground under my player so ican make sure he is grounded
void checkForGround(){
isGrounded = Physics2D.OverlapCircle (groundCheck.position,checkRadius,whatisGround);
}
//this function flips the player to other direction right to left
void Flip(){
isFacingRight = !isFacingRight;
Vector3 theScale = transform.localScale;
theScale.x *= -1;
transform.localScale = theScale;
}
}