Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 Tibllec · Mar 11 at 03:49 AM · scripting problemtriggergravityparentparent and child

How to disable player gravity

My script makes it that whenever you enter an area of ​​gravity (a collider) you enter the gravity of the planet.

What I want is when you leave, you can be in zero gravity, but what the code is doing is when you leave the planet, you continue to be affected by the planet's gravity. I want a way to set the players gravity off when you leave the planet.

This must be because gravity areas become the player's parent whenever it enters another gravity area. Well I'm really stuck here

 public class GravityOrbit : MonoBehaviour
 {
     public float Gravity;
 
     // Start is called before the first frame update
     private void OnTriggerEnter(Collider other)
     {
         if (other.GetComponent<GravityCtrl>())
         {
             other.GetComponent<GravityCtrl>().Gravity = this.GetComponent<GravityOrbit>();
         }
 
         else
 
         {
             
         }
     }
 
     private void OnTriggerExit(Collider other)
     {
             
     }
 
     // Update is called once per frame
     void Update()
     {
         
     }
 }

Gravity

 public class GravityCtrl : MonoBehaviour
 {
     public GravityOrbit Gravity;
     public Rigidbody Rb;
 
     public float RotationSpeed = 20;
 
     void Start()
     {
         Rb = GetComponent<Rigidbody>();
     }
 
     void FixedUpdate()
     {
         if (Gravity)
         {
             Rb.transform.parent = Gravity.transform;
 
             Vector3 gravityUp = Vector3.zero;
 
             gravityUp = (transform.position - Gravity.transform.position).normalized;
 
             Vector3 localUp = transform.up;
 
             Quaternion targetRotation = Quaternion.FromToRotation(localUp, gravityUp) * transform.rotation;
             Rb.GetComponent<Rigidbody>().rotation = targetRotation;
 
             transform.rotation = Quaternion.Lerp(transform.rotation, targetRotation, RotationSpeed * Time.deltaTime);
             Rb.GetComponent<Rigidbody>().AddForce((-gravityUp * Gravity.Gravity) * Rb.mass);
         }
 
         else
 
         {
 
         }
     }
 
 }

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

2 Replies

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

Answer by Tibllec · Mar 11 at 05:05 PM

Oh god, what a day, ok, the only thing i had to do was set the OnTriggerExit in the gravityCtrl code, and in it put Gravity = null;

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
1

Answer by aqeel25 · Mar 11 at 04:30 AM

   private void OnTriggerEnter(Collider other)
      {
 other.transform.GetComponent<Rigidbody>().useGravity = false;
 }
  private void OnTriggerExit(Collider other)
      {
              other.transform.GetComponent<Rigidbody>().useGravity =true;
      }

Comment
Add comment · Show 3 · 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 Tibllec · Mar 11 at 04:58 AM 0
Share

Hi @aqeel25 , sorry for the misunderstanding.

I want a way to turn off players gravity when you leave the planet.

The code you used uses unity gravity, but I had to use a different gravity because of the hyspheric world

avatar image aqeel25 Tibllec · Mar 11 at 05:05 AM 0
Share

Ok, then try reparenting it in statement where it exits collision. I hope this will work. Give it try

avatar image Tibllec aqeel25 · Mar 11 at 05:54 AM 0
Share

Hey! I managed at the beginning of the game for the player to be off the planet, and only start to be affected by gravity when entering its collider. (now the player is no longer a child of gavity forever)

But I found that if the player is affected by gravity once, gravity continues to affect him even if he leaves the collider.

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

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

Apply gravity to parents 1 Answer

time dilation & gravity Effect Not working propperly 1 Answer

How to prevent a game object from colliding with its parent 2 Answers

Reaching a gameobject from its collider 3 Answers

How do I move it along with the platform while still keeping it's original scale!! 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