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 /
  • Help Room /
avatar image
0
Question by Semystic · Nov 10, 2020 at 05:38 PM · gravityballtunnel

Gravity to inside walls of tube?

Just a heads up, im a beginner trying to make a game thats a bit ambitious for my skill level so be easy on me.

Basically I have a tube of a certain length, a wall object at the back end, and a ball object. I want the ball to stick to the inside walls of the tunnel and move freely and bounce off the back wall and come back. Ive searched for a long time and the closest thing i could find that kinda worked was this:

 public float wallGravity = -9.81f;
     void OnCollisionStay(Collision col)
     {
         Vector3 n = Vector3.zero;
         foreach (ContactPoint C in
             col.contacts)
             n += C.normal;
         n.Normalize();
 
         Physics.gravity = -n * wallGravity;
     }

Not exactly sure how it works but it acts crazy and doesnt really work well. Also it sticks itself to the back wall when it hits instead of bouncing off (i have bouncy physics material on back wall). So basically im looking for suggestions on a different way to stick the ball to the tunnel but not the back wall. Ive heard raycasting would work, but i know jack about that. If thats the best course, ill gladly learn about it. Anything would be helpful!

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 Semystic · Dec 19, 2020 at 03:35 AM

For those wondering, i found something that works much better. Could use some tweeking but its great. It rotates around the z axis of the tube rather than trying to force it to the inside walls. It runs a lot smoother too.

 transform.RotateAround(tube.transform.position, Vector3.forward, 0);
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 xxmariofer · Nov 17, 2020 at 01:37 PM

the easiest solution is to add a tag to the backwall and inside the collisionevent check if its colliding with the wall

      void OnCollisionStay(Collision col)
      {
          if(col.gameObject.tag == "BackWall") return;
          Vector3 n = Vector3.zero;
          foreach (ContactPoint C in
              col.contacts)
              n += C.normal;
          n.Normalize();
  
          Physics.gravity = -n * wallGravity;
      }
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 Semystic · Nov 24, 2020 at 08:28 PM 0
Share

Okay that part works, thank you. However the gravity part of the script is still garbage and doesnt work well. The ball seems to propel itself around the tube, but thats after it freaks out and starts flying around bouncing off the walls a few times. Do you have a better solution to that problem?

avatar image Semystic · Nov 24, 2020 at 10:04 PM 0
Share

So i tried something else that seems to work much much better, but it seems to be pulling towards the back wall at all times.

 public Rigidbody rb;
     void Start()
     {
         rb = GetComponent<Rigidbody>();
     }
     void OnCollisionEnter(Collision collision)
     {
         if (collision.gameObject.tag == "backWall") return;
 
             ContactPoint contact = collision.GetContact(0);
             Vector3 position = contact.point;
             rb.AddForce(position);
     }

Any help would be appreciated!

avatar image xxmariofer Semystic · Nov 25, 2020 at 08:29 AM 0
Share

Without seeing the project is hard to tell, but i would try disabling gravity on collision enter and reenabling it on collision exit in case thats the force pulling towards the back

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

219 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

Related Questions

Position Ball 2 Answers

My ball fall throw the ground ? 0 Answers

Physics for ball on an incline: rolling up a hill if half way up 1 Answer

How to make a player instantly reach to a change in gravity? 2 Answers

After added a rigidbody in a GameObject, it moves on rotation 2 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