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
1
Question by Reefer · Sep 14, 2013 at 10:23 AM · c#physicsballmagnetic

Magnetic ball

I'm wondering how should I do an magnetic ball for my game. I'm in process of learning C# so any tips/maybe some starting code (as I don't have any clue where to start with this) would be appreciated in that language, but JS isnt problem either, I can always translate it to C#.

Magnetic ball? I'm meaning this kind of ball that old PS1 classic had: "Kula World"

"Video of Kula World"

Meaning that it can't drop off the level if you don't jump off, and you can go even walls straight up, or you can go under the level and it won't drop, but still if you're under the level you can jump. Watch the video of Kula World and you'll understand.

Comment
Add comment · Show 3
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 vexe · Sep 14, 2013 at 10:41 AM 0
Share

I find this to be interesting (faved). I would have definitely upvoted the question if you'd thrown some attempts and ideas to begin with ins$$anonymous$$d of asking in an open-discussion manner. (I'm sure if you took the time to think about it, something will come out of your thinking process)

avatar image Reefer · Sep 14, 2013 at 02:47 PM 0
Share

Well, I've tried it with physic materials but this was ofcourse an no go. Then I tried to stick it on objects with fixed joint, but this causes that ball can't move at all.

I tried to stick it to the floor with this code:

     using UnityEngine;
     using System.Collections;
      
     public class StickyObject : $$anonymous$$onoBehaviour
     {
     void OnCollisionEnter(Collision c) {
     var joint = gameObject.AddComponent<FixedJoint>();
     joint.connectedBody = c.rigidbody;
     }
 }

but it causes that ball can't move at all ofcourse, spring joint is getting nearer, but it's not good as it tries to get you back to the starting point always so you can't really move at all and so on. I know these are stupid ways to try to do this, but I can't really come up with anything else.

I guess rotating the whole level always from trigger point could do it, but it's not really the way I'm looking for.

avatar image vexe · Sep 14, 2013 at 04:35 PM 0
Share

I thought about using a kinematic rigidbody, gravity is handled manually, jump -> decrease vertical velocity by Physics.gravity.y * Time.deltaTime / 2 or something... about rotating, I thought about rotating the whole level like you said, or keeping that and rotating the ball based on what surface it's stuck on...

1 Reply

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

Answer by JoaquinRD · Sep 14, 2013 at 03:20 PM

In your ball script:

 public float jumpForce = 5f;
 
 GameObject currentSurface;
 
 ConstantForce _gravityOffset;
 ConstantForce gravityOffset
 {
     get
     {
         if (_gravityOffset == null)
         {
             _gravityOffset = gameObject.AddComponent<ConstantForce>();
         }
 
         return _gravityOffset;
     }
     
     set { _gravityOffset = value; }
 }
 
 void OnCollisionEnter(Collision c)
 {
     //set object currently sticking to
     currentSurface = c.gameObject;
 }
 
 void OnCollisionStay(Collision c)
 {
     if (c.gameObject == currentSurface)
     {
         //get contact point
         ContactPoint contactPoint = c.contacts[0];

         //set new gravity along the normal of contact point
         gravityOffset.force = (-1f * Physics.gravity) + (-1f * contactPoint.normal);
     }
 }
 
 void OnCollisionExit(Collision c)
 {
     //leaving the surface
     if (c.gameObject == currentSurface)
     {
         currentSurface = null;
         gravityOffset.force = Vector3.zero;
     }
 }
 
 void Jump()
 {
     //don't jump if not on a surface
     if (currentSurface == null) return;
 
     //add jump force away from current surface
     rigidbody.AddForce(-1f * gravityOffset.force * jumpForce);
 
     //return to normal gravity
     gravityOffset.force = Vector3.zero;
 }
Comment
Add comment · Show 19 · 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 vexe · Sep 14, 2013 at 04:41 PM 0
Share

This looks good, makes sense. Is it tested? I'm gonna try and mess with this...

avatar image JoaquinRD · Sep 14, 2013 at 04:47 PM 0
Share

I didn't test it, just an idea :)

avatar image vexe · Sep 14, 2013 at 05:50 PM 1
Share
  • O$$anonymous$$, it is working to some extent, but require some hacking, there are some inconsistencies, but nevertheless, you made it look easy man! :) alt text

grav.png (43.3 kB)
avatar image vexe · Sep 14, 2013 at 05:54 PM 1
Share

Could you put some light on this line?

 gravityOffset.force = (-1f * Physics.gravity) + (-1f * contactPoint.normal);
avatar image JoaquinRD · Sep 14, 2013 at 06:02 PM 1
Share

Sure thing.

Physics.gravity is a Vector3 that represents the gravity(force) applied to all ridigbodies in the scene. In order to negate the force of gravity, we add the opposite vector (-1f * Physics.gravity) to the ConstantForce.

Then, to set the ball's gravity towards the surface that it is sticking to, we first need the ContactPoint from the Collision. This ContactPoint holds the normal of the contact (contactPoint.normal), which is a Vector3 that points outward from the surface, perpendicular to the surface. Gravity towards that surface would be in the opposite direction (-1f * contactPoint.normal).

Finally, adding these two vectors together gives us a Vector that both counteracts the force of gravity applied by Physics.gravity and produces a new gravitational force toward the surface.

Hope this made sense...

Show more comments

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

20 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

Related Questions

Making the ball be in front of me 1 Answer

Scripted Ball Bouncing igher and higher 0 Answers

How can I show trajectory of a bouncing ball OnCollisionEnter/OnCollisionExit? 0 Answers

Show trajectory of a bouncing ball 0 Answers

Multiple Cars not working 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