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
1
Question by Jorn Jilesen · Dec 07, 2015 at 08:37 AM · 2dgravityfauxgravity

faux gravity in 2d game

Hey guys,

I need a faux gravity to a circle in a 2d game, so the player can walk on it. Is this possible? Does anyone know a tutorial that can help me out?

Thanks!

Comment
Add comment · Show 1
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 Pangamini · Dec 07, 2015 at 10:33 AM 0
Share

just apply force on your object, pointing to the center of the circle

3 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by Eno-Khaon · Dec 07, 2015 at 11:12 AM

There are plenty of introductions around for 3D and 2D implementations of this, and for similar topics.

That said, having made a habit out of doing this in 3D in some form for many of my personal projects, I can give you a few tips on how to get started:

First, gravity: You'll probably want to give every physics-driven moving object in your scene its own personal gravitation script. In this script, it would make sense to require a Rigidbody2D, since the whole point is to replace it. In this script, turn off gravity for the object (in the case of 2D, set gravityScale to 0), define a Transform (or Vector2) to attract the object towards, then in FixedUpdate(), apply a gravitational force towards the position of (target.position - thisObject.position).normalized * gravitationalForce where "gravitationalForce" would be given a default value of 9.81 (or -9.81, depending on implementation).

By applying that force on each object towards their defined gravitational center point, you can have everything pulled towards anything else you want. Further developments on the concept usually begin to involve Raycasts and surface normals when you begin to involve irregular shapes, but that's another topic entirely.

Second, rotation: The 2D approach is much, much, much, much, much simpler than the 3D approach to handling rotation and camera control. I'm afraid I can't offer any specific details, as I'm not confident in my knowledge of 2D Unity axes, but I can tell you that your rotation will often, most likely be perpendicular to the gravitational pull. That means ensuring that your character's forward vector is equal to either (-y, x) or (y, -x), depending on whether you're facing left or right.

Hopefully this will be able to at least get you started on the right track. Good luck!

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
avatar image
0

Answer by Jorn Jilesen · Dec 11, 2015 at 12:34 PM

Thank you so much! I will give it a try! :)

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
avatar image
0

Answer by unity_6ZMJlhFpaWH3QA · Jul 02, 2019 at 12:29 AM

@Eno-Khaon

what about jumping? any idea about how to prevent the player from flying away?

this is the code i use for jumping

    void Jump(){
     Vector2 vertical = new Vector2(rb.velocity.x, JumpSpeed).normalized;
     rb.velocity = rb.transform.position + rb.transform.TransformDirection((vertical) * MoveSpeed * Time.deltaTime);
     rb.velocity = rb.transform.position + rb.transform.TransformDirection((-vertical) * MoveSpeed * Time.deltaTime);

}

this the code i use for moving, it may interfer one in another

     void Run(int directionOnX)
 {
     Vector2 horizontal = new Vector2(directionOnX, rb.velocity.y).normalized;
     rb.MovePosition(rb.transform.position + rb.transform.TransformDirection((horizontal) * MoveSpeed * Time.deltaTime));
 }
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 Optrix · Jul 02, 2019 at 03:38 AM 0
Share

Don't add your current position to your velocity! Adding your position to it is going to make for some REALLY strange effects - the further you are from the center of the world, the faster you'll jump away from it :P

If you've added gravity, jumping will be solved - the only way you'll have trouble is if you give it WAY too much jump force and you simply fling off into space...but if your gravity is correct you'll eventually be pulled back to earth :P

One trick - make sure you're only firing the jump once. If you're just checking for the jump button being pressed (ie. using GetButton() rather than GetButtonDown()), you'll run the Jump function several times and you'll rocket away insanely fast.

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

7 People are following this question.

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

Related Questions

Can I access Physics2dSettings in Project Settings via script 1 Answer

Instantiated prefab changes gravitational speed when dragged? 0 Answers

How to walk on 2d sphere (planet) 2 Answers

Problem of gravity 1 Answer

Gravity Walk on Walls and Roof 2D 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