K-digital traning/유니티 기초

[수업과제] 표창 던지기

내꺼블로그 2023. 8. 1. 13:05

1. Unity

 

※ Scene 생성 -> Shuriken Object 생성 -> C# script(ShurikenController) 생성 -> script를 object의 component로 붙이기

 

 

 

2. 스크립트

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

public class ShurikenController : MonoBehaviour
{
    //이동, 회전 속도(초기값 : 0)
    float moveSpeed;
    float rotAngle;
    float dampingCoefficient = 0.96f;

    Vector3 startPos;

    // Start is called before the first frame update
    void Start()
    {
        
    }

    // Update is called once per frame
    void Update()
    {
        if (Input.GetMouseButtonDown(0))
        {
            Debug.LogFormat("startPos: {0}", Input.mousePosition);
            Debug.LogFormat("position: {0}", this.transform.position);
            startPos=Input.mousePosition;       //마우스 클릭 시 화면좌표 저장
        }
        else if (Input.GetMouseButtonUp(0))
        {
            Debug.LogFormat("endPos: {0}", Input.mousePosition);
            
            Debug.LogFormat("position: {0}", this.transform.position);
            Vector3 endPos = Input.mousePosition;       //마우스 버튼 up 시 화면좌표 저장
            float swipe = endPos.y - startPos.y;        //y좌표 사이 거리 저장
            
            //위의 값으로 이동 및 회전 속도 결정
            this.moveSpeed = swipe / 1000;
            this.rotAngle = swipe / 10;
            Debug.LogFormat("moveSpeed: {0}", moveSpeed);
            Debug.LogFormat("rotAngle: {0}", rotAngle);
        }

        //위에 결정된 속도 만큼 이동 및 회전
        this.transform.Translate(0, moveSpeed, 0, Space.World);		//world 사용 => 공간 축
        this.transform.Rotate(0, 0, rotAngle);


        //감쇠(서서히 느리게)
        this.moveSpeed *= dampingCoefficient;
        this.rotAngle *= dampingCoefficient;
    }
}

 

 

 

 

3.결과(쨔잔)