카테고리 없음

[게임 매니악스 슈팅 게임 알고리즘] 테이블을 이용한 방향탄 Unity 로 따라하기

Kim2558 2019. 1. 1. 18:05

게임 매니악스 슈팅 게임 알고리즘에 나오는 방향탄 코드 2-11를 Unity에서 테스트해봤습니다. 


실시간으로 Sin, Cos 연산을 하지 않고 미리 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
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
 
public class Table : MonoBehaviour {
 
    public Vector2[] posArray;
 
    private void Awake()
    {
        for(int  i = 0; i <360; i ++)
        {
            posArray[i].x = Mathf.Cos(Mathf.PI / 180 * i);
            posArray[i].y = Mathf.Sin(Mathf.PI / 180 * i);
        }
    }
 
    public Vector2 GetSpeedVector(int theta)
    {
        theta %= 360;
        return posArray[theta];
    }
 
}
 
cs


미리 계산된 값을 기즈모로 그려본 모양 



결과