유니티 기초

수리검 던지기

송현호 2023. 8. 1. 12:50
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class shurikenSwipe : MonoBehaviour
{
    float moveSpeed = 0;
    Vector3 startPos;
    Vector3 endPos;
    float damplingCoefficient = 0.98f;
    // Start is called before the first frame update
    void Start()
    {
        
    }

    // Update is called once per frame
    void Update()
    {
        if (Input.GetMouseButtonDown(0))
        {
            Debug.LogFormat("Down: {0}", Input.mousePosition);
            this.startPos = Input.mousePosition;
        }

        else if (Input.GetMouseButtonUp(0))
        {
            Debug.LogFormat("Down: {0}", Input.mousePosition);
            this.endPos = Input.mousePosition;
            float swipeLength = endPos.y - startPos.y;
            this.moveSpeed = swipeLength/1000;
        }

        this.transform.Translate(0, moveSpeed, 0, Space.World);
        if (moveSpeed > 0)
        {
            this.transform.Rotate(0f, 0f, 10f);
        }
        
        moveSpeed *= damplingCoefficient;
    }
}

startPos와 endPos변수에 마우스를 눌렀을때와 뗐을 때의 좌표를 저장해서 이동한 y축의 길이만큼 표창을 날리기로 했다.

 

수리검을 날렸을 때 오브젝트의 좌표계도 같이 돌면서 표창이 이상한 곳으로 날라갔지만 월드 좌표계를 사용하여 해결하였다.