Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 yveris · Oct 20, 2013 at 05:45 AM · 2dphysicsrigidbody2d-platformergravity

[Complex] How to move the player similar to character controller while also applying rigidbody physics vectors

I am making a 2D platformer that involves the player being manipulated by gravitational spheres of influence. These gravity wells add force to the player in the direction of the force vector that gravity would apply in real life to objects. Obviously I've magnified the power of gravity since it is such a weak force. It is based on this equation Fg = GM1M2/|r^2| * r/|r|.

My issue is rooted in player movement and player jumping. I want to be able to have the player's ground running velocity translate into his jumping velocity, as well as increase his movement speed without giving the player the ability to overpower the force vectors of the gravity wells.

Currently movement is using a function of translation that cannot overpower the forces of my artificial gravity acting on it in the horizontal direction; it can only slightly empower or weaken it but cannot change direction unless the horizontal velocity vector has a very small magnitude.

When this translation gets higher than the power of the vector it causes issue with gameplay mechanics.

So I guess my problem can be boiled down to the question: Can I get advise on how one might go about creating a workaround or creating realistic force vectors based on the way that a player would run and jump.

An issue I have run into in my programming is that if I am simply applying vectors I do not like the slide effect that the player gets, I want a 2D-platformer style stop on a dime type deal, if anything he must stop very quicklyI guess I could keep him moving very slow but I would rather empower the player to make very quick and fluid motions so that players with high reaction times can have fun going fast, while not compromising mechanics and allowing methodical and slower players to also enjoy the game.

The player currently moves in this fashion:

 enter code herebool isGrounded;
     Vector3 startPosition, moveAmount;
     float distToGround;
     public float jumpForce, moveSpeed;
     float horizontal, vertical;
     // Use this for initialization
     void Start () {
         distToGround = collider.bounds.extents.y;
         startPosition = transform.position;
         isGrounded = false;
     }
     void jump(){
         if(isGrounded){
             this.transform.rigidbody.AddForce(Vector3.up * jumpForce);    
         }
     }
     void checkGrounding(){
         if(Physics.Raycast(transform.position, -Vector3.up, distToGround +0.3f))
             isGrounded = true;
     }
     void OnCollisionEnter(Collision coll){
             if(coll.gameObject.name.Equals("Plane")){
                 checkGrounding();
                 if(isGrounded){
                     startPosition = transform.position;
                 }
             }
     }
     void OnCollisionExit(Collision coll){
             if(coll.gameObject.name.Equals("Plane"))
                 isGrounded = false;
     }
     // Update is called once per frame
     void FixedUpdate () {
         horizontal = Input.GetAxis("Horizontal");
         vertical = Input.GetAxis("Vertical");
         moveAmount = transform.right * horizontal * moveSpeed;
 
         if(Input.GetKeyDown(KeyCode.R)){
             transform.position = startPosition;
             transform.rigidbody.angularVelocity = Vector3.zero;
             transform.rigidbody.velocity = Vector3.zero;
         }
         if(horizontal!=0)
                     this.transform.Translate(Vector3.right*(horizontal/4.3f));
         if(Input.GetAxis("Jump")!=0)
             jump();
     }
  
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 yveris · Oct 20, 2013 at 06:55 AM 0
Share

addendum: When using rigid body controls and making the user unable to move post-jump in a realistic system it makes the game so impossible. All entry trajectories must be pre-calculated. It's very difficult.

avatar image yveris · Oct 20, 2013 at 07:22 AM 0
Share

Additional question: is it possible to calculate the translation of a moving jump and store that and apply that throughout the jump.

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by yveris · Oct 20, 2013 at 08:40 AM

If there is not a more sophisticated fashion of calculating this sort of movement the vector of translation can be saved in the jump function like so:

 void jump(){
             if(isGrounded){
                 if (horizontal != 0)
                     if (Input.GetKey(KeyCode.LeftShift))
                     {
                         if (Physics.Raycast(transform.position, -Vector3.up, distToGround + 0.3f))
                             movingVector = Vector3.right * (horizontal);
                     }
                     else
                     {
                       movingVector = (Vector3.right * (horizontal / 4.3f));
                     }
                 this.transform.rigidbody.AddForce(Vector3.up * jumpForce);
                 isMoving = true;
             }
         }

This stores a boolean that during the fixed update keeps applying until is is grounded again.

This is checked as such:

         void checkGrounding(){
             if(Physics.Raycast(transform.position, -Vector3.up, distToGround +0.3f)){
                 isGrounded = true;
                 isMoving = false;
             }
         }

and is continually applied until the grounding is called again in the OnCollisionEnter function.

And the update function is edited and adds at the beginning this if statement; such that the user can still make slight alterations through the walking speed which is the targeted amount of alteration.

         if (isMoving){
             transform.Translate(movingVector);
         }

I still think this is a bit overly complicated and if anyone can find a simpler and eloquent answer I would be much obliged.

Comment
Add comment · Show 1 · 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 yveris · Oct 20, 2013 at 08:55 AM 0
Share

If anything this question seems really specific but maybe it'll help people who are trying to make 2D platformers that need rigidbody physics but realstic constraints are causing an issue or they still want a mario type style of jump.

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

15 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

Related Questions

Sprite does not rotate with a CircleCollider2D 1 Answer

Setting a RigidBody's velocity messes with my custom gravity, not sure how to proceed. 0 Answers

Help with orbits 1 Answer

Can I access Physics2dSettings in Project Settings via script 1 Answer

Rigidbody (with Box Collider) falls through floor 1 Answer


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