for a better understanding, do you know the (360 degree 2d android game) where a ball bounces in a circle and it doesn’t go out? that’s what i 'm trying to do i have 2 sprites 1 big ball and one small ball that is within the big one and what i 'm trying to do is detect when the small ball is out of the big circle ball,i don’t want a whole explanation for how to make it move and stay in the circle radius,i just want to detect when its out of the big circle radius
Get the distance from the big ball center to the small ball center (minus the small radius, depending if it has to be completely outside or not). If this value is greater than the big balls radius it’s outside.
distance from center1 to center2(-small radius) > big radius: small ball is outside
It is simple:
using UnityEngine;
using System.Collections;
public class BallOutside : MonoBehaviour
{
public Transform smallBall;
public Transform bigBall;
public float smallBallRadius = 1f;
public float bigBallRadius = 5f;
private float radiusDiff;
void Start()
{
radiusDiff = bigBallRadius - smallBallRadius;
}
/// <summary>
/// If small ball is outside then returns true.
/// </summary>
public bool IsSmallBallOutside()
{
if((smallBall.position - bigBall.position).magnitude <= radiusDiff)
return false;
return true;
}
void Update()
{
if(IsSmallBallOutside())
Debug.Log("Outside");
else
Debug.Log("Inside");
}
void OnDrawGizmos()
{
if(smallBall == null || bigBall == null)
return;
Gizmos.DrawWireSphere(smallBall.position, smallBallRadius);
Gizmos.color = Color.red;
Gizmos.DrawWireSphere(bigBall.position, bigBallRadius);
}
}