Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

128
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!