Unity/Study
[Unity] Sin 함수를 통한 반복 운동
Kim2558
2018. 1. 18. 11:14
Sin 그래프
Sin 그래프는 1, -1 을 반복 한다. 이를 이용하여 반복 움직임을 구현할 수 있다.
반복 움직임 코드
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | using System.Collections; using System.Collections.Generic; using UnityEngine; using UniRx; using UniRx.Triggers; public class Test : MonoBehaviour { [Header("속도, 길이")] [SerializeField] [Range(0f,10f)] private float speed = 1f; [SerializeField] [Range(0f,10f)] private float length = 1f; private float runningTime = 0f; private float yPos = 0f; // Use this for initialization void Start() { this.UpdateAsObservable() .Subscribe(_ => { runningTime += Time.deltaTime * speed; yPos = Mathf.Sin(runningTime) * length; Debug.Log(yPos); this.transform.position = new Vector2(0,yPos); }); } } | cs |
실행 결과
인스펙터 창의 length, speed를 조정해 거리, 속도를 조절할 수 있다.