- Home /
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"
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.
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)
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.
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...
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;
}
This looks good, makes sense. Is it tested? I'm gonna try and mess with this...
O$$anonymous$$, it is working to some extent, but require some hacking, there are some inconsistencies, but nevertheless, you made it look easy man! :)
Could you put some light on this line?
gravityOffset.force = (-1f * Physics.gravity) + (-1f * contactPoint.normal);
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...
Your answer
![](https://koobas.hobune.stream/wayback/20220613114136im_/https://answers.unity.com/themes/thub/images/avi.jpg)