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 /
This question was closed Apr 09, 2016 at 06:47 AM by tanoshimi for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by Vujukas · Apr 09, 2016 at 06:28 AM · 2dunity5jump

Realistic Jumping Help

Hi, im trying to implement a realistic movement for my game. The code i have yet looks like this: (GroundedCheck does work, so thats not the issue):

 using UnityEngine;
 using System.Collections;
 
 public class Player : MonoBehaviour
 {
 
     //Movment Variables
     private float moveX;
     private float moveY;
     public float moveSpeed;
     public float jumpSpeed;
     public bool isGrounded;
 
     // Update is called once per frame
     void FixedUpdate()
     {
 
         if (Input.GetKey(KeyCode.A) && isGrounded)
         {
             transform.Translate(new Vector2(1, 0) * -moveSpeed * Time.deltaTime);
         }
 
         if (Input.GetKey(KeyCode.D) && isGrounded)
         {
             transform.Translate(new Vector2(1, 0) * moveSpeed * Time.deltaTime);
         }
 
         if (Input.GetKey(KeyCode.Space) && isGrounded)
         {
             GetComponent<Rigidbody2D>().velocity = new Vector2(GetComponent<Rigidbody2D>().velocity.x, jumpSpeed);
         }
 
     }
 
 }

The problem is that i don't want the character to be able to move left or right while he is in the air, i get this to work with the grounded check also the character cant jump when already in the air. The thing is if i walk and jump the character just jumps vertically and lands exactly where he started, i want a jump in the direction where the character is walking in.

Do you know how to implement this ?

Comment
Add comment · Show 2
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 Vujukas · Apr 08, 2016 at 11:40 PM 0
Share

Ok i found a kind of solution, it now works like i want but i have to see how the animation look later, maybe the character slides too much

 void FixedUpdate()
     {
 
         if (Input.Get$$anonymous$$ey($$anonymous$$eyCode.A) && isGrounded)
         {
             GetComponent<Rigidbody2D>().AddForce(new Vector2(1, 0) * -moveSpeed);
             //transform.Translate(new Vector2(1, 0) * -moveSpeed * Time.deltaTime);
         }
 
         if (Input.Get$$anonymous$$ey($$anonymous$$eyCode.D) && isGrounded)
         {
             GetComponent<Rigidbody2D>().AddForce(new Vector2(1, 0) * moveSpeed);
             // transform.Translate(new Vector2(1, 0) * moveSpeed * Time.deltaTime);
         }
 
         if (Input.Get$$anonymous$$ey($$anonymous$$eyCode.Space) && isGrounded)
         {
             GetComponent<Rigidbody2D>().AddForce(new Vector2(0, 1) * jumpSpeed * 10);
             //GetComponent<Rigidbody2D>().velocity += jumpSpeed * Vector2.up;
         }
 
     }
avatar image Vujukas · Apr 09, 2016 at 12:54 AM 0
Share

I fixed everything, if anyone is interested:

 public class Player : $$anonymous$$onoBehaviour
 {
 
     public float maxSpeed = 10f;
     public float jumpForce;
     public bool facingRight = true;
     public bool isGrounded;
 
     void Start()
     {
 
     }
 
     void FixedUpdate()
     {
         if (!isGrounded)
         {
             return;
         }
 
         float move = Input.GetAxis("Horizontal");
 
         GetComponent<Rigidbody2D>().velocity = new Vector2(move * maxSpeed, GetComponent<Rigidbody2D>().velocity.y);
 
         if (move > 0 && !facingRight)
         {
             Flip();
         }
         else if (move < 0 && facingRight)
         {
             Flip();
         }
     }
 
     void Update()
     {
         if (isGrounded && Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.Space))
         {
             GetComponent<Rigidbody2D>().AddForce(new Vector2(0, 1 * jumpForce * 20));
         }
     }
 
     void Flip()
     {
         facingRight = !facingRight;
         Vector3 theScale = transform.localScale;
         theScale.x *= -1;
         transform.localScale = theScale;
     }
 
 }

0 Replies

  • Sort: 

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

2D Sprite.Create isn't working 0 Answers

how long a bottom being pressed [solved] 3 Answers

Unity2D Player Jump Heights Inconsistent,Unity2D Inconsistent Player Jump Heights 0 Answers

Input.GetButtonDown("Jump") doesn't work 0 Answers

Why do prefabs overlap each other? 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