Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Question on implementing a "radar" rotating object.

Discussion in 'Scripting' started by maxxjr, Jul 29, 2020.

  1. maxxjr

    maxxjr

    Joined:
    Dec 10, 2017
    Posts:
    13
    Consider what is essentially a radar.

    I want the "radar" to rotate at a certain speed, say 20 degrees per second. No problem with how to do that.

    But...I want to scan at fixed intervals, such as every one degree.

    Any suggestions on how to structure this?

    I'm early in the process of thinking about how to do this and searching forums for people doing similar things.

    Thanks!

    My early thoughts on how to do this:
    The rotation is linked to Time.deltaTime, which will be different on different systems...how fast they run things. At each Time.deltaTime, I can do the rotation, and then calculate if at least one degree of rotation is past, and if so, do the scan. On a slow system, if the Update is happening at less than 20 degrees/second, then I have to adjust to some slower scan interval, such as every 2 degrees.
     
  2. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,735
    Shouldn't matter. Your want your radar to rotate at the same real world speed no matter what the framerate is. That's the point of using Time.deltaTime in the first place.

    Nah, if necessary, do multiple scans in a single frame. Something like this should work:

    Code (CSharp):
    1. [SerializeField]
    2. Vector3 rotationAxis;
    3.  
    4. [SerializeField]
    5. float rotationSpeed; // Degrees per second
    6.  
    7. [SerializeField]
    8. float scanInterval; // Degrees per scan
    9.  
    10. float rotationAccumulator = 0;
    11.  
    12. // This variable is just in case your scans need to know what direction to scan in.
    13. float currentRotation = 0;
    14.  
    15. void Start() {
    16.   rotationAxis.Normalize();
    17. }
    18.  
    19. void Update() {
    20.   float degreesToRotate = rotationSpeed * Time.deltaTime;
    21.   transform.Rotate(rotationAxis * degreesToRotate);
    22.  
    23.   rotationAccumulator += degreesToRotate;
    24.   while (rotationAccumulator >= scanInterval) {
    25.     rotationAccumulator -= scanInterval;
    26.  
    27.     // increment the scanning direction
    28.     currentRotation += scanInterval;
    29.  
    30.     // perform the actual scan (with a certain rotation)
    31.     DoAScan(currentRotation);
    32.   }
    33. }
    34.  
    35. void DoAScan(float atThisRotation) {
    36.   // Whatever code you need to perform a scan.
    37. }
     
  3. maxxjr

    maxxjr

    Joined:
    Dec 10, 2017
    Posts:
    13
    This helped a lot. Thank you!