My player is flying up in the air when i start my game.
Heres the code Please see if you can help me
using UnityEngine; using System.Collections;
public class PlayerController : MonoBehaviour {
public float moveSpeed;
public float JumpHeight;
public Transform groundCheck;
public float groundCheckRadius;
public LayerMask whatIsGround;
private bool grounded;
private bool doubleJumped;
private Animator anim;
// Use this for initialization
void Start()
{
anim = GetComponent<Animator>();
}
void FixedUpdate()
{
grounded = Physics2D.OverlapCircle(groundCheck.position, groundCheckRadius, whatIsGround);
}
// Update is called once per frame
void Update()
{
if (grounded)
doubleJumped = false;
anim.SetBool("Grounded", grounded);
if (Input.GetKeyDown(KeyCode.Space) && grounded) ;
{
GetComponent<Rigidbody2D>().velocity = new Vector2(GetComponent<Rigidbody2D>().velocity.x, JumpHeight);
}
if (Input.GetKeyDown(KeyCode.Space) && !doubleJumped && !grounded)
{
GetComponent<Rigidbody2D>().velocity = new Vector2(GetComponent<Rigidbody2D>().velocity.x, JumpHeight);
doubleJumped = true;
}
if (Input.GetKey(KeyCode.D))
{
GetComponent<Rigidbody2D>().velocity = new Vector2(moveSpeed, GetComponent<Rigidbody2D>().velocity.y);
}
if (Input.GetKey(KeyCode.A))
{
GetComponent<Rigidbody2D>().velocity = new Vector2(-moveSpeed, GetComponent<Rigidbody2D>().velocity.y);
}
anim.SetFloat("Speed", Mathf.Abs(GetComponent<Rigidbody2D>().velocity.x));
{
if (Input.GetKeyDown(KeyCode.Escape) == true)
{
Application.Quit();
}
}
}
}
Answer by Zoogyburger · Mar 04, 2016 at 06:47 PM
Hey that script is almost exactly like mine!
private Rigidbody2D m_Rigidbody;
public float m_Speed ;
public float m_JumpHeight ;
public Transform groundCheck;
public float groundCheckRadius;
public LayerMask whatIsGround;
private bool grounded;
private bool doubleJumped;
// Use this for initialization
void Start () {
// Use this for initialization
m_Rigidbody = GetComponent<Rigidbody2D>();
}
void FixedUpdate(){
grounded = Physics2D.OverlapCircle (groundCheck.position, groundCheckRadius, whatIsGround);
}
// Update is called once per frame
void Update () {
if (grounded)
doubleJumped = false;
if (Input.GetKeyDown (KeyCode.Space)&& grounded) {
m_Rigidbody.velocity = new Vector2 (m_Rigidbody.velocity.x, m_JumpHeight);
}
if (Input.GetKeyDown (KeyCode.Space) && !doubleJumped && !grounded) {
m_Rigidbody.velocity = new Vector2 (m_Rigidbody.velocity.x, m_JumpHeight);
doubleJumped = true;
}
if (Input.GetKey (KeyCode.D)) {
m_Rigidbody.velocity = new Vector2 (m_Speed, m_Rigidbody.velocity.y);
}
if (Input.GetKey (KeyCode.A)) {
m_Rigidbody.velocity = new Vector2 (-m_Speed, m_Rigidbody.velocity.y);
}
}
}
Yea but $$anonymous$$e doesn't work XD when i start the game the player just flies up in the sky ;_;
Well my script doesn't do that, so maybe you want to combine them.
I will try that .-. (maby thanks will see if i can make something work here)
I combined them, see if this works
private Rigidbody2D m_Rigidbody;
public float m_Speed ;
public float m_JumpHeight ;
public Transform groundCheck;
public float groundCheckRadius;
public Layer$$anonymous$$ask whatIsGround;
private bool grounded;
private bool doubleJumped;
private Animator anim;
// Use this for initialization
void Start () {
// Use this for initialization
anim = GetComponent<Animator>();
m_Rigidbody = GetComponent<Rigidbody2D>();
}
void FixedUpdate(){
grounded = Physics2D.OverlapCircle (groundCheck.position, groundCheckRadius, whatIsGround);
}
// Update is called once per frame
void Update () {
if (grounded)
doubleJumped = false;
anim.SetBool("Grounded", grounded);
if (Input.Get$$anonymous$$eyDown ($$anonymous$$eyCode.Space)&& grounded) {
m_Rigidbody.velocity = new Vector2 (m_Rigidbody.velocity.x, m_JumpHeight);
}
if (Input.Get$$anonymous$$eyDown ($$anonymous$$eyCode.Space) && !doubleJumped && !grounded) {
m_Rigidbody.velocity = new Vector2 (m_Rigidbody.velocity.x, m_JumpHeight);
doubleJumped = true;
}
if (Input.Get$$anonymous$$ey ($$anonymous$$eyCode.D)) {
m_Rigidbody.velocity = new Vector2 (m_Speed, m_Rigidbody.velocity.y);
}
if (Input.Get$$anonymous$$ey ($$anonymous$$eyCode.A)) {
m_Rigidbody.velocity = new Vector2 (-m_Speed, m_Rigidbody.velocity.y);
anim.SetFloat ("Speed", $$anonymous$$athf.Abs (GetComponent<Rigidbody2D> ().velocity.x));
}
if (Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.Escape) == true)
{
Application.Quit();
}
}
}
Inside the Inspector on your player's Rigidbody2D You might want to set the Gravity scale to 5 and have a boxcollider2D run along the entire floor (also a boxcollider2D on the player).
Your answer
![](https://koobas.hobune.stream/wayback/20220612070603im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
How to deal with grounded check after jump initiated? 3 Answers
How to make so that the player can jump on a object, but he can't stand on it? (2D) 0 Answers
Changing a variable from a different method 0 Answers
Values not updating in Unity. - UPDATED 1 Answer
Trying to code a knockback but the player is teleporting instead. 0 Answers