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.
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;
}