Search Unity

Limiting to area outside of circle

Discussion in 'Scripting' started by CryptoJanne, Jan 16, 2022.

  1. CryptoJanne

    CryptoJanne

    Joined:
    Jul 7, 2019
    Posts:
    12
    Hello everyone.

    test2.gif

    I want the aim/laser to stop at the circle. Right now i have this:

    Code (CSharp):
    1. var (success, position) = GetMousePosition();
    2.         if (success)
    3.         {
    4.             Vector3 direction;
    5.             float distance = (aimTransform.position - position).magnitude;
    6.            
    7.             if (distance < aimCircle)
    8.             {
    9.                 direction = position - aimTransform.position;
    10.                 direction.Normalize();
    11.                 direction.y = 0;
    12.             } else
    13.             {
    14.                 direction = position - aimTransform.position;
    15.             }
    16.          
    17.             aimTransform.forward = direction;
    18.         }
    I have tried to set direction.y to a good amount, but that does not work. Any input that helps me solve this would make me extremely happy.
    I have also tried playing around with Vector3.ClampMagnitude, but it only has maxDistance, and not minDistance as i would need. I lack understanding in math to figure it out.
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,689
    Don't you just need to flip the sense of line 7 to read
    if (distance > aimCircle)
    ?

    Your lines 9 to 11 keep it constrained at 1 radius
     
  3. AnimalMan

    AnimalMan

    Joined:
    Apr 1, 2018
    Posts:
    1,164
    The code you posted doesn’t show us where the length of the laser is scaled. Is it a line renderer? Because wherever you are scaling the laser to the mouse position is where you’ll need to input the minimum range. It appears that the minimum range defaults to some huge range when the mouse is inside the circle, and hits mouse position when outside. So it’s not that the mouse doesn’t behave right when inside the circle because it does, it’s that the line or length of the laser does not know that it needs to be limited to the circles radius and likewise the code provided doesn’t show this part this all involves the direction of the laser.
     
  4. CryptoJanne

    CryptoJanne

    Joined:
    Jul 7, 2019
    Posts:
    12
    I might have failed to describe what i want, I am sorry. If the cursor is inside the circle, i want the lazor(aim) to be clamped at the circles line. Making the player aim a bit forward(from its own perspective) instead of straight down.
     
  5. CryptoJanne

    CryptoJanne

    Joined:
    Jul 7, 2019
    Posts:
    12
    The laser stops when it hits something, and is following the mouse originating from the barrel. But if it is inside the circle, i am pointing it straight forward, just for showing purposes. It is showing the direction of the guns barrel.