Notice
Recent Posts
Recent Comments
Link
«   2024/05   »
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
Archives
Today
Total
관리 메뉴

게임 프로그래밍

[Unity] AddForce 궤도 구하기 본문

프로그래밍/유니티

[Unity] AddForce 궤도 구하기

Junwe 2020. 3. 3. 21:30
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
 private void setTrajectoryPoints(Vector3 pStartPosition, Vector3 pVelocity) // 시작 위치, addForce에 들어는 
    {
        float velocity = Mathf.Sqrt((pVelocity.x * pVelocity.x) + (pVelocity.y * pVelocity.y)); // 속도 구하기
        float angle = Mathf.Rad2Deg * (Mathf.Atan2(pVelocity.y, pVelocity.x));  // 각도 구하기
        float fTime = 0;        
        int tragectoryNum = 10// 궤도 개수
        fTime += 0.1f;
        for (int i = 0; i < tragectoryNum; i++)
        {
            float dx = velocity * fTime * Mathf.Cos(angle * Mathf.Deg2Rad);
            float dy = velocity * fTime * Mathf.Sin(angle * Mathf.Deg2Rad) - (Physics2D.gravity.magnitude * fTime * fTime / 2.0f);
            Vector3 pos = new Vector3(pStartPosition.x + dx, pStartPosition.y + dy, -2);
            trajectoryPoints[i].transform.position = pos;
            trajectoryPoints[i].gameObject.SetActive(true);
            trajectoryPoints[i].transform.eulerAngles = new Vector3(00, Mathf.Atan2(pVelocity.y - (Physics.gravity.magnitude) * fTime, pVelocity.x) * Mathf.Rad2Deg); // 보고있는 각도 계산
            fTime += 0.1f;
        }
    }
cs

Unity에서 사용하는 AddForce에 궤적을 구하는 코드이다. 속도와 각도를 구한후 시간마다 위치 값을 계산해준다.

Comments