Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by Sillenkiwi · Mar 04, 2016 at 06:24 PM · 2d-platformerground

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();
         }
     }
 }

}

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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);
 
 
         }
 
 
     }
 
 
 
 
 } 
Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Sillenkiwi · Mar 04, 2016 at 06:56 PM 0
Share

Yea but $$anonymous$$e doesn't work XD when i start the game the player just flies up in the sky ;_;

avatar image Zoogyburger · Mar 04, 2016 at 07:03 PM 0
Share

Well my script doesn't do that, so maybe you want to combine them.

avatar image Sillenkiwi Zoogyburger · Mar 04, 2016 at 07:28 PM 0
Share

I will try that .-. (maby thanks will see if i can make something work here)

avatar image Zoogyburger · Mar 04, 2016 at 07:40 PM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

51 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges