- Home /
Unity 4.3 - 2D demo - question about jumpForce
Hi everyone,
Unity newb here. I have a question about adapting the PlayerControl script in Unity's 2D demo.
I took the most basic stuff - move left, right, jump - from the script, but I'm having problems with the jumping. Here's a video: http://www.youtube.com/watch?v=7EQvnB-7MD8 Any ideas on why this is happening? I've pretty much copied the script verbatim.
The script is below. I'm also having problems with checking whether or not the player is grounded, but one thing at a time. Code tips welcome. I've also attached an image of my rigidBody2D object properties. Thanks all.
using UnityEngine;
using System.Collections;
public class PlayerControl : MonoBehaviour {
[HideInInspector]
public bool facingRight = true; //to determine which way the player is currently facing
[HideInInspector]
public bool jump = false; //test condition for whether or not the player should jump
public float moveForce = 365f; //amount of force added to move player left and right
public float maxSpeed = 5f; //the fastest the player can travel in the x-axis
public AudioClip[] jumpClips; //an array of audio clips to play for jump sound effects
public float jumpForce = 1000f; //the amount of force added when the player jumps
public Transform groundCheck;
public bool grounded = false;
// Use this for initialization
void Start () {
}
void Awake() {
groundCheck = transform.Find ("groundCheck");
Debug.Log ("groundCheck:" + groundCheck);
}
// Update is called once per frame
void Update () {
// The player is grounded if a linecast to the groundcheck position hits anything on the ground layer.
grounded = Physics2D.Linecast(transform.position, groundCheck.position, 1 << LayerMask.NameToLayer("Ground"));
grounded = true;
// Debug.Log ("grounded: " + grounded);
if(Input.GetButtonDown("Jump") && grounded) { //spacebar by default
jump = true;
}
}
void FixedUpdate() {
float h = Input.GetAxis("Horizontal");
if(h * rigidbody2D.velocity.x < maxSpeed)
// ... add a force to the player.
rigidbody2D.AddForce(Vector2.right * h * moveForce);
if(Mathf.Abs(rigidbody2D.velocity.x) > maxSpeed)
// ... set the player's velocity to the maxSpeed in the x axis.
rigidbody2D.velocity = new Vector2(Mathf.Sign(rigidbody2D.velocity.x) * maxSpeed, rigidbody2D.velocity.y);
// If the input is moving the player right and the player is facing left...
if(h > 0 && !facingRight)
// ... flip the player.
Flip();
// Otherwise if the input is moving the player left and the player is facing right...
else if(h < 0 && facingRight)
// ... flip the player.
Flip();
if(jump) {
rigidbody2D.AddForce(new Vector2(0f, jumpForce));
jump = false;
}
}
void Flip () {
// Switch the way the player is labelled as facing.
facingRight = !facingRight;
// Multiply the player's x local scale by -1.
Vector3 theScale = transform.localScale;
theScale.x *= -1;
transform.localScale = theScale;
}
}
Answer by antk · May 14, 2014 at 05:08 PM
For future reference - for anyone else looking at this - it was because I didn't have my gravity settings set up the same as the Unity 4.3 2D demo.
Edit -> Project Settings -> Physics 2D
Then update the y value in the Gravity settings.
Answer by KellyThomas · Dec 14, 2013 at 10:20 AM
Your code says:
grounded = Physics2D.Linecast(transform.position, groundCheck.position, 1 << LayerMask.NameToLayer("Ground"));
grounded = true;
if(Input.GetButtonDown("Jump") && grounded) { //spacebar by default
jump = true;
}
You are overwriting the result of you linecast and forcing grounded to true on every frame. As a consequence you are applying jumpForce for each frame the jump button is held down, and flying into the air.
Thanks, but I don't think that's the issue. I think the grounded flag is intended to prevent the character from double/triple/quadruple/etc jumping. If I take out the check for the "grounded" state, my character will still jump really, really high. If I take the check out from Unity's 2D sample, it simply allows their bean character to jump multiple times in the air.
I discovered that if I reduce the jumpForce, the jump looks better. I just don't understand why it works properly in Unity's 2D sample, but not when I try to apply it to my scene.
Your right, I was confusing the behaviour of Input.GetButtonDown()
with Input.GetButton()
.
The peak y position can be found using:
peak = -1 * (v^2) / (2 * G) // where v is launch velocity.
So with a jumpForce of 1000, the jump will peak at approx 51000.
We can find initial velocity required to achieve a desired height, by rearranging this forumla to the following:
v = sqrt( -2 * G * peak)
For example if G = -9.8
and peak = 2
, it resolves to v = 6.26
.
Thank you, I changed the jump force and it's better now. Now I'm having problems recycling my platform prefabs.
Answer by jackpriceburns · Dec 14, 2013 at 08:59 AM
Are you using a CharacterController because if you are then you can easily make a jump here's my code for my first person shooter. It has jump but it uses the character controller.
using UnityEngine;
using System.Collections;
public class FirstPerson_Controller : MonoBehaviour {
float mouseSpeed = 2.5f;
float movementSpeed = 0;
float verticalRotation = 0;
float upDownRange = 60.0f;
float verticalVelocity = 0;
float jumpSpeed = 6.5f;
// Use this for initialization
void Start () {
Screen.lockCursor = true;
}
// Update is called once per frame
void Update () {
CharacterController cc = GetComponent<CharacterController>();
float rotX = Input.GetAxis("Mouse X") * mouseSpeed;
transform.Rotate (0, rotX, 0);
verticalRotation -= Input.GetAxis ("Mouse Y") * mouseSpeed;
verticalRotation = Mathf.Clamp(verticalRotation, -upDownRange, upDownRange);
Camera.main.transform.localRotation = Quaternion.Euler(verticalRotation, 0, 0);
//Movement
if(cc.isGrounded && Input.GetButton("Jump")){
verticalVelocity = jumpSpeed;
}
if(Input.GetButton("Fire1") && cc.isGrounded){
movementSpeed = 15.0f;
} else {
movementSpeed = 7.5f;
}
float forwardSpeed = Input.GetAxis("Vertical") * movementSpeed;
float sideSpeed = Input.GetAxis("Horizontal") * movementSpeed;
verticalVelocity += Physics.gravity.y * Time.deltaTime;
Vector3 speed = new Vector3( sideSpeed, verticalVelocity, forwardSpeed);
speed = transform.rotation * speed;
cc.Move (speed * Time.deltaTime);
}
}
Your answer
Follow this Question
Related Questions
Animation sets z.position to 0 0 Answers
Even / Constant Speed and Jumping 2D 1 Answer
I want my player to jump only once. How?, 1 Answer
How can I make my character jump & move him by zone 0 Answers