Search Unity

2D Radar Rotate

Discussion in '2D' started by The3DKnight, Oct 17, 2017.

  1. The3DKnight

    The3DKnight

    Joined:
    Feb 8, 2011
    Posts:
    240
    Hello, there I'm having a bit of trouble
    .
    I used code that I found on Unity Tutorial, about rotating around a pivot point, from point A to point B.
    I understand the basic concept of Rotation.

    Code (CSharp):
    1. int angle = (int)pivot.transform.eulerAngles.z;
    2.  
    3. void Start(){
    4. pivot.transform.Rotate(0.0f, 0.0f, Random.Range(-70.0f,70.0f));
    5. //Sets starting point Random between -70 and 70 degrees
    6.  
    7. }
    8.  
    9. void Update(){
    10.         int angle = (int)pivot.transform.eulerAngles.z;
    11.  
    12.         if (angle > 80 || angle < -80)
    13.         angle -= 360;
    14.  
    15.         if ((angle <= -80) || (angle >= 80))
    16.         direction *= -1f; // reverse direction of radar
    17.  
    18.         pivot.transform.Rotate (0, speed * direction * Time.deltaTime);
    19. }
    20.  

    https://ibb.co/k4pzLm

    What I want is for red laser to rotate from Current position 0 to 90, and then from 90 back to -90 etc.Like a radar would do.
    This code manages to do that but gets stuck sometimes at 81f. Is there a better way to do this?

    And I noticed angle in Editor is shown like from 0 to 90 and from 0 to -90. While in play mode it's from 0 to 90 and when I want it to go back it's from 0 to 270.Does that affect it?

    I'm making some basic mobile game, so any help or tip would be appreciated. Thanks