게임 클라이언트 프로그래밍

표창 던지기

park-gom 2024. 8. 20. 12:07

 

회전을 위해 .Rotate 메서드와

움직임을 위해 .translate 메서드를 사용했는데

표창이 똑바로 날아가지 못하고 이상하게 도는 현상을 발견했다

 

예측되는 문제는 translate이 단순히 position의 좌표를 이동시키는것이 아니라

오브젝트의 방향에 따라 좌표의 이동에 영향을 준다는것이다.

 

그래서 translate메서드를 빼버리고 그냥 좌표자체를 변경하는 방식으로 구현하였다.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class kunaiController : MonoBehaviour
{
    float speed = 0;
    float rotSpeed = 0;
    Vector2 startPos;

    void Start()
    {

    }

    void Update()
    {
        // 스와이프 길이를 구한다
        if (Input.GetMouseButtonDown(0))
        {
            // 마우스를 클릭한 좌표
            this.startPos = Input.mousePosition;
        }
        else if (Input.GetMouseButtonUp(0))
        {
            // 마우스 버튼에서 손가락을 떼었을 때 좌표
            Vector2 endPos = Input.mousePosition;
            float swipeLength = endPos.y - this.startPos.y;

            // 스와이프 길이를 처음 속도로 변환한다
            this.speed = swipeLength / 1000.0f;

            // 회전 속도 설정
            this.rotSpeed = 10;

        }

        // 회전 속도만큼 표창을 회전 시킨다
        transform.Rotate(0, 0, this.rotSpeed);
        this.transform.position += new Vector3(0, this.speed, 0);

        this.speed *= 0.98f;                    // 감속
        this.rotSpeed *= 0.99f;
    }
}

'게임 클라이언트 프로그래밍' 카테고리의 다른 글

프로세스 스레드 코루틴  (0) 2024.08.24
carSwipe  (0) 2024.08.20