Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

129
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda