Mathf.Sin jerks when changing speed mid game
Trying to make it so depending on the score depends on the speed of the Sine. But when I change the speed mid game the game object that is using the script will jerk to a different part of the wave (Hope that makes sense). Anyways of fixing this or do I need to use a different way (If so what would you recommend)?
 public float delta = 1.5f;  // Amount to move left and right from the start point
      public float speed;
      public float direction = 1;
      private Quaternion startPos;
 
      float timer;
 
      Score score;
 
      void Start()
      {
          startPos = transform.rotation;
          score = FindObjectOfType<Score>();
      }
 
     void FixedUpdate()
     {
         timer += Time.deltaTime;
     }
 
     void Update()
      {
          
 
          if (score.score <= 3)
          {
              speed = transform.position.z / 9f;
          }
          else if (score.score <= 8)
          {
              speed = transform.position.z / 11.5f;
          }
          else {
              speed = transform.position.z / 13f;
          }
 
          Quaternion a = startPos;
 
         a.y += direction * (delta * Mathf.Sin(timer * speed));
         transform.rotation = a;
 
     }
 
              
               Comment
              
 
               
              Your answer
 
             Follow this Question
Related Questions
How to make projectile to shoot in a sine wave pattern 1 Answer
A graph to render sine wave with custom resolution 0 Answers
How do you change the velocity of an object moving in a sine wave? 0 Answers
Sin function returns NaN 1 Answer
Swimming in a sinusoidal fashion, positive cycle out of water and negative within. Am I close? 0 Answers