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 /
avatar image
0
Question by BlankSlates · Sep 15, 2018 at 08:57 PM · rigidbody2djumpingforcebegginerplatformercontroller

I am a begginer trying to make a simple 2d platformer. Need help with rotation and jump height.

So, I am a beginner. Just started attempting to make a platformer today instead of following planned projects for once. . I was trying my best to work through it without to much copy and pasting of code and using it to build my knowledge as a beginner. I have gotten confused on rotating the sprite when walking left and right though. Could you tell me how to make my code work for this ? Atm with how it is now it will rotate in that direction at a fixed speed when i run and it just keeps turning around in circles. Going nowhere fast. lol I estimate this question is probably pretty simple to solve. As I'm obviously just not using the correct method.

My second question however i think is a bit tougher. Basically with the code set up in the current way i can get my sprite to jump pretty consistently without double jumps. Which is better then what i have had in the past. But what i need to do is completely normalize the jumping "thrust". With things how they are now every once in awhile i will get a high jump anywhere between 2 to 5 times my normal jump height. This cant happen if im trying to jump between platforms and have crates to stack in order to navigate longer jumps like i have set up now in my sample scene that i just started working on. Is there a way to set a failsafe to make sure the jumps stay consistant in height and distance with a rigidbody 2d like i have now ? Or better yet is there a way to do that as a beginner without being completely confused as to how it works. Thanks for your time and hope to hear back on this. I am sure my first question is kind of stupid but cant seem to figure it out.

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.Animations;
 using UnityEngine.UI;
 using UnityEngine.SceneManagement;

 public class playerController : MonoBehaviour
 {
     [SerializeField]
     private Animator playeranim;
     private Rigidbody2D rb2d;
    // private BoxCollider2D box2d;
     [SerializeField]
     private int thrust = 100;
     [SerializeField]
     private int jumpHeight = 50;
     [SerializeField]
     private int canJump;
     private Vector2 direction;
 

     // Use this for initialization
         void Start ()
     {
         rb2d = gameObject.GetComponent<Rigidbody2D>();
        // box2d = gameObject.GetComponent<BoxCollider2D>();
     }
 
     // Update is called once per frame
     void Update ()
     
     {
         RotateDirection();
         playeranim = gameObject.GetComponent<Animator>();
         rigidbodymovement();

    
     }

     private void rigidbodymovement()
     {
     

         if (Input.GetKey(KeyCode.D))

         {

             playeranim.SetInteger("running", 2);


             rb2d.AddForce(transform.right * thrust * Time.deltaTime);

         }

         else if (Input.GetKey(KeyCode.A))
         {
        

             playeranim.SetInteger("running", 2);


             rb2d.AddForce(-transform.right * thrust * Time.deltaTime);



         }

         else
             playeranim.SetInteger("running", 0);
         playeranim.SetInteger("jump", 0);


         if (Input.GetKeyDown(KeyCode.Space))  // todo fix double jump and a glitch where the player 
 turns his body when jumping and controls glitch
         {
             if( canJump > 1)
             {
                 canJump = canJump - 1;
             
                 rb2d.AddForce(new Vector2(0, 1) * jumpHeight * Time.deltaTime);
             }
         
         }

     
     {
     
     }




     // }

     //else
     //{
     //  playeranim.SetBool("is_Running", false);
     //  playeranim.SetBool("is_Idle", true);
     // }
 }
     private void OnCollisionEnter2D(Collision2D collision)
     {
         switch ( collision.gameObject.tag)
         {
             case "water":
                 {
                     Invoke("death", 1f);
                     break;
                 }
             case "floor":
                 {
                     Debug.Log("i am touching the floor");
                     canJump = 2;
                     break;
                 }

             default:
                 {
                     canJump = 1;
                     break;
                 }
         }
     }

     private void RotateDirection()

      {
      transform.Rotate(direction);

      direction = Vector2.zero;
   
     if (Input.GetKey(KeyCode.A))
      {
             direction += new Vector2(0, 1);
      }
  
     if (Input.GetKey(KeyCode.D))
       {
             direction += new Vector2(0,1 );
      }



     }

      private void death()
     {
     

         SceneManager.LoadScene(0);
     }
 }
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
Best Answer

Answer by jandd661 · Sep 15, 2018 at 09:51 PM

Greetings, For your jump issue, use`Mathf.Clamp` to clamp the Y velocity. Example: rb2d.velocity = new Vector2(rb2d.velocity.x, Mathf.Clamp(rb2d.velocity.y, -10f, 10f)); Where the 0f is the min velocity and the 10f is the max velocity. Play around with the 10f to get something that feels right. Then move it to a variable private serialized field. Place the snippet at the beginning of FixedUpdate()


Also, use FixedUpdate() to apply forces to your rigidbody. I would move the call to rigidbodymovement() from Update() to FixedUpdate.


In Update() you have playeranim = gameObject.GetComponent<Animator>(); that should be in Awake() or Start() as it is right now, you are cache the animator component every frame.


As far as the the rotate thing, Are you rotating a ball or something or turning the player to a new direction?


You are off to a great start and welcome to the mind numbing adventures of programing! Don't ever give up.

Comment
Add comment · 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

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

94 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 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

increasing knockback of a rigidbody as received damage increases (like super smash) 1 Answer

Rigidbody 2d add force horizontally affecting vertical velocity 1 Answer

Unity 2D Apply force in Z Direction 0 Answers

Double Jump Not working 2 Answers

How to not get velocity by the other objects?,How to not get force by other gameobjects? 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