Unity/Study

[Unity] sin, cos 함수를 이용한 원 운동

Kim2558 2018. 1. 19. 19:57

Sin, Cos 그래프





Cos 값이 x 축, Sin 값이 y 축에 입력되면 x 축은 -2π 부터 -π 까지 좌측으로 이동하게 된다. 이 때 y 축은 -2π 부터 -3/2π 까지 올라가다  -3/2π부터  -π 까지 내려간다. 즉 두 x,y 값이  반원을 그리게 된다. 

마찬가지로 밑의 반원도 같은 원리로 그려진다


원 운동 코드


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
30
31
32
33
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 = 1;
    [SerializeField] [Range(0f, 10f)] private float radius = 1;
 
    private float runningTime = 0;
    private Vector2 newPos = new Vector2();
 
    // Use this for initialization
    void Start()
    {
 
        this.UpdateAsObservable()
            .Subscribe(_ =>
            {
                runningTime += Time.deltaTime * speed;
                float x = radius * Mathf.Cos(runningTime);
                float y = radius * Mathf.Sin(runningTime);
                newPos = new Vector2(x, y);
                this.transform.position = newPos;
 
            });
    }
}
 
cs


실행 결과