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.결과(쨔잔)
'K-digital traning > 유니티 기초' 카테고리의 다른 글
몬스터 처지 후 아이템 드롭 + 장착 (0) | 2023.08.10 |
---|---|
[주말과제] 이동 및 몬스터 공격 (0) | 2023.08.07 |
애니메이션 전환 연습 (0) | 2023.08.03 |
캐릭터 이동(달리기) 애니메이션 (0) | 2023.08.03 |
ClimbCloud (0) | 2023.08.02 |