I have sphere named Pointer that can move everywhere with transform.Translate() but i want this sphere can only move inside a half-sphere named LimitSphere, i don't know how to do it.
i already tried to use Mathf.Clamp but as you can see on this image
but when i set an inscribed square i loose too much space (yellow)
and when i set an circumscribed square there is too much space out of the circle (purple/pink)
So is there any solution to limit a movement inside sphere ?
EDIT : That's my actual code :
using UnityEngine;
using System.Collections;
public class TranslationClavier : MonoBehaviour {
public float vitesse_translation = 1.0f;
void Update () {
if (Input.GetKey(KeyCode.DownArrow))
{
transform.Translate(Vector3.up * vitesse_translation * Time.deltaTime);
}
if (Input.GetKey(KeyCode.UpArrow))
{
transform.Translate(Vector3.down * vitesse_translation * Time.deltaTime);
}
if (Input.GetKey(KeyCode.RightArrow))
{
transform.Translate(Vector3.right * vitesse_translation * Time.deltaTime);
}
if (Input.GetKey(KeyCode.LeftArrow))
{
transform.Translate(Vector3.left * vitesse_translation * Time.deltaTime);
}
if (Input.GetKey(KeyCode.I))
{
transform.Translate(Vector3.forward * vitesse_translation * Time.deltaTime);
}
if (Input.GetKey(KeyCode.K))
{
transform.Translate(-Vector3.forward * vitesse_translation * Time.deltaTime);
}
}
}
under you can see my little sphere and i want to clamp her inside the big sphere collider
i finally did this
// Get the new position for the object.
Vector3 movement = new Vector3(Input.GetAxis("Vertical"), Input.GetAxis("VerticalJD"), Input.GetAxis("Horizontal")) * vitesse_translation;
Vector3 newPos = transform.position + movement;
// Calculate the distance of the new position from the center point. Keep the direction
// the same but clamp the length to the specified radius.
Vector3 offset = newPos - centerPt;
transform.position = (centerPt + Vector3.ClampMagnitude(offset, radius));