Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

133
Vistas
Restrict gameobject inside a radius around my player

I need to restrict my Players hand inside a radius around the character. The game is 2D and the hand movement is basically exactly the same as: Madness Interactive (https://www.newgrounds.com/portal/view/118826)

[SerializeField] private float speed = 2f;
[SerializeField] private GameObject radiusCenter;
[SerializeField] private float radius = 0.5f;


void Start()
{
    Cursor.visible = false;
}



void Update()
{

    Vector3 playerPos = radiusCenter.transform.position;
    Vector3 playerToHand = this.transform.position - playerPos;


    var moveDirection = new Vector3(Input.GetAxis("Mouse X"), Input.GetAxis("Mouse Y"), 0);

    float DistanceToRadius = Vector3.Distance(playerPos, playerToHand);


    transform.position += moveDirection * speed * Time.deltaTime;
    
    

    float angle = Mathf.Atan2(playerToHand.y, playerToHand.x) * Mathf.Rad2Deg;
    transform.rotation = Quaternion.AngleAxis(angle, Vector3.forward);

}

I cant figure out the way to keep my players hand inside a set radius around my player, see pictures below for further explanation:

enter image description here enter image description here enter image description here

I thought maybe I could get a float value from playerPos and playerToHand and create a IF-statement that restricts the hand from going outside the set radius float, but I didnt get it to work...

EDIT 1: The hand is a child of my player. I use a gameObject in the center of my player as radiusCenter which my hand rotates around.

over 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

One very simple way to check if the hand is too far away is to use this line of code:

<movement code here>
if (Vector3.distance(playerPos, transform.position) > radius)
{
    transform.localPosition = transform.localPosition.normalized * radius;
{

This will make it so if the hand will be outside the circle after the move, it will just put the hand on the circle.

over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda