Ingen beskrivning
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Move.cs 859B

12345678910111213141516171819202122232425262728293031323334353637
  1. using UnityEngine;
  2. public class Move : MonoBehaviour
  3. {
  4. Vector3 pos;
  5. public float speed = 10.0f;
  6. float min = -15f;
  7. float max = 15f;
  8. private float waitTime = 0;
  9. private float timer = 0;
  10. private float addedSpeed = 0;
  11. void Start()
  12. {
  13. pos = transform.position;
  14. waitTime = 1 / speed;
  15. }
  16. void Update()
  17. {
  18. var dt = Time.deltaTime;
  19. timer -= dt;
  20. if (timer < 0)
  21. {
  22. timer = waitTime;
  23. var timeForMove = Time.time;
  24. if (waitTime < dt)
  25. {
  26. timeForMove += (dt - waitTime);
  27. }
  28. addedSpeed = Mathf.Abs(Mathf.Sin(timeForMove * 1.5f) * (max - min));
  29. var z = (Mathf.PingPong((addedSpeed), max - min) + min);
  30. transform.position = new Vector3(pos.x, pos.y, z);
  31. }
  32. }
  33. }