sin, cos 활용 예시
sin, cos을 활용하여
다양한 각도를 적용하여 패턴을 쉽게 만들 수 있다
단순하게 좌우로 움직이게 하려면
sin의 패턴을 활용해
DegreeComponent를 아래처럼 작성할 수 있다
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class DegreeComponent : MonoBehaviour
{
[Range(1, 4)]
public float scalar = 0.0f;
[Range(0, 360)]
public float degree = 0.0f;
float dTime;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
dTime += Time.deltaTime;
float delta = scalar * Mathf.Sin(dTime);
// 방향 (x, y)
float x = delta * Mathf.Cos(Degree2Rad(degree));
float y = delta * Mathf.Sin(Degree2Rad(degree));
Vector3 dirVec = new Vector3(x, y, 0);
transform.position = dirVec;
}
float Degree2Rad(float degree)
{
return degree * Mathf.PI / 180;
}
}
Degree를 0도로 설정해 주면 위의 결과물은 아래와 같다
그리고 이러한 오브젝트를 여러 개 생성하고
그에 맞는 각도를
예를들어 0도, 180도, 90도, 270도로 각각 조정해 주면
아래와 같은 결과물이 나오게 된다
계속 원의 형태로 돌도록 만들고 싶다면
아래와 같은 SpinComponent를 작성해서
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class SpinComponent : MonoBehaviour
{
[Range(1, 4)]
public float scalar = 0.0f;
[Range(0, 360)]
public float degree = 0.0f;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
degree += Time.deltaTime * 50.0f;
// 방향 (x, y)
float x = scalar * Mathf.Cos(Degree2Rad(degree));
float y = scalar * Mathf.Sin(Degree2Rad(degree));
Vector3 dirVec = new Vector3(x, y, 0);
transform.position = dirVec;
}
float Degree2Rad(float degree)
{
return degree * Mathf.PI / 180;
}
}
그에 맞는 scalar와 degree를 설정해 주면
아래와 같이 결과물이 나온다
'게임 수학 & 물리 > 게임 수학' 카테고리의 다른 글
[게임 수학] 벡터의 기초 (2) (0) | 2023.10.17 |
---|---|
[게임 수학] 벡터의 기초 (1) (0) | 2023.10.17 |
[게임 수학] tangent (0) | 2023.10.16 |
[게임 수학] sin, cos (1) (0) | 2023.10.16 |
[게임 수학] 피타고라스의 정리 (0) | 2023.10.15 |