Search Unity

moving enemy randomly with smoothing

Discussion in 'Scripting' started by Glowball123, Feb 6, 2021.

  1. Glowball123

    Glowball123

    Joined:
    Mar 10, 2016
    Posts:
    78
    Code (CSharp):
    1.  void Alternate()
    2.     {
    3.      
    4.        
    5.            
    6.             if (t > duration)
    7.             {
    8.                 Change();
    9.             }
    10.             else
    11.             {
    12.                 t += Time.deltaTime;
    13.                 amount = Mathf.SmoothStep(cp.Angle, destination, Mathf.Abs(duration));
    14.             }
    15.        
    16.     }
    17.  
    18.     void Change()
    19.     {
    20.         t = 0;
    21.         duration = Random.Range(0.5f, 3f);
    22.         random = Random.Range(0, 2) * 2 - 1;
    23.         duration *= random;
    24.         destination = ((Time.deltaTime * duration) * rotateSpeed) + cp.Angle;
    25.         print("NEW");
    26.     }
    the code above is my attempt so far (Change() gets called in start and Alternate() gets called in update) what i want to do is rotate my enemy around a cylinder randomly, with smoothing applied. but instead it just moves it increasingly fast in only one direction. How should i go about solving this?
     
  2. arfish

    arfish

    Joined:
    Jan 28, 2017
    Posts:
    782
    Hi,

    Put in some Debug.Logs to see what is happening. To see if for example the duration value, and destination are what you expect them to be. Perhaps the Change() method is never called?