Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Restricting cursor to a radius around my Player

Tags:

c#

unity3d

I am currently working on an multiplayer shooter game. Basicly atm you play as a cube and you have your hand(red square) following the cursor ingame.

I want to restrict the cursor movement to a perfect circle around my player sprite.

See attached picture for clarification.

https://imgur.com/TLliade

Following script is attached to the "Hand" (red square).

public class Handscript : MonoBehaviour
{

    void Update()
    {
        Cursor.visible = false;

        Vector3 a = Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, 0));
        a.Set(a.x, a.y, transform.position.z);
        transform.position = Vector3.Lerp(transform.position, a, 1f);
    }
}

Now I want to restrict cursor movement inside my radius that is a public transform attached to my Player prefab like this:

    //Hand radius
    public float radius;
    public Transform centerRadius;

I am hard stuck and new to coding overall and I could use a push in the right direction.

Basicly the same question is asked here if I am being to unclear: https://answers.unity.com/questions/1439356/limit-mouse-movement-around-player.html

EDIT: My goal in the end is to have similar hand movement as in the legendary "Madness Interactive" game, found here: https://www.newgrounds.com/portal/view/118826

EDIT2: It might be impossible to lock the cursor inside the radius circle. Is it possible to just have the GameObject "Hand" locked inside this radius?

EDIT3: This is the code I use and it works like a charm:

using System;
using UnityEngine;

public class Handscript : Photon.MonoBehaviour
{
    [SerializeField] private GameObject Player2;        //Drag your player game object here for its position
    [SerializeField] private float radius;              //Set radius here

    public new PhotonView photonView;                   //Never mind this if its not a photon project

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

    void Update()
    {
            if (photonView.isMine)      //Never mind this if statement if it isnt a photon project
            {
                Vector3 cursorPos = Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, 0));
                Vector3 playerPos = Player2.transform.position;

                Vector3 playerToCursor = cursorPos - playerPos;
                Vector3 dir = playerToCursor.normalized;
                Vector3 cursorVector = dir * radius;

                if (playerToCursor.magnitude < cursorVector.magnitude) // detect if mouse is in inner radius
                    cursorVector = playerToCursor;

                transform.position = playerPos + cursorVector;

            }
    }




#if UNITY_EDITOR
    private void OnDrawGizmosSelected()
    {
        UnityEditor.Handles.DrawWireDisc(transform.parent.position, Vector3.back, radius); // draw radius
    }
#endif
}

like image 268
Sam Andersson Avatar asked Aug 21 '19 14:08

Sam Andersson


1 Answers

  1. Get vector from the player to the cursor:

    Vector3 playerToCursor = cursorPos - playerPos;
    
  2. Normalize it to get the direction:

    Vector3 dir = playerToCursor.normalized;
    
  3. Multiply direction by your desired radius:

    Vector3 cursorVector = dir * radius;
    
  4. Add the cursor vector to the player position to get the final position:

    Vector3 finalPos = playerPos + cursorVector;
    

pic

like image 61
Iggy Avatar answered Nov 14 '22 23:11

Iggy