1

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.

  • Maybe this helps you to figure out the logic [how to limit and clamp distance between two points in a Line renderer unity2d](https://stackoverflow.com/questions/56317745/how-to-limit-and-clamp-distance-between-two-points-in-a-line-renderer-unity2d) – derHugo Mar 29 '21 at 17:12

1 Answers1

2

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.

Simonster
  • 593
  • 4
  • 10
  • I tried your code with some minor tweaks:if (Vector3.Distance(playerPos, transform.position) < radius) The issue now is that as soon as i reach the radius outer limit the hand just stops moving at all... I just want it to be restricted inside the circle, but still move even tho its on the outer most edge! – Sam Andersson Mar 29 '21 at 17:15
  • I your hand a child of your player? – Simonster Mar 29 '21 at 17:19
  • Yes! I should mention that in the question. – Sam Andersson Mar 29 '21 at 17:20
  • Ok. Then, you can use the code I wrote but instead of putting your moving code inside it, you should put `transform.localPosition = transform.localPosition.normalized * radius` so if the hand is out of range, it will just put it back to the circle around your player. – Simonster Mar 29 '21 at 17:26
  • Hmm, now the hand wont enter the circle. I think it should be the other way around! I want to restrict the hand inside the circle. Now its outside – Sam Andersson Mar 29 '21 at 17:29
  • Then I think the condition is wrong because then it is only putting the hand on the circle if it is inside the circle. They reversing the sign. – Simonster Mar 29 '21 at 17:32
  • Ah now it works! I had the "<" wrong! Can you please Edit your answer and I accept it as the solution! – Sam Andersson Mar 29 '21 at 17:37
  • if (Vector3.Distance(playerPos, transform.position) > radius) transform.localPosition = transform.localPosition.normalized * radius; – Sam Andersson Mar 29 '21 at 17:38
  • 2
    Ok. Finished Editing. – Simonster Mar 29 '21 at 17:40