using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class shurikenController : MonoBehaviour
{
float moveSpeed = 0;
float rotationSpeed = 1000f; // 회전 속도
float dampingCoefficient = 0.96f; //감쇠계수
private Vector3 startPos; //down 했을때 위치
// 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("Up: {0}", Input.mousePosition);
Vector3 endPos = Input.mousePosition;
float swipeLength = endPos.y - startPos.y;
Debug.LogFormat("swipeLength: {0}", swipeLength); //화면좌표에서 두점사이의 거리
//화면좌표계에서의 두점사이의 거리에 비례해서 이동 좌표계가 안맞아서 어림잡아 500정도 나눠줬다
this.moveSpeed = swipeLength / 500f;
Debug.LogFormat("moveSpeed: {0}", this.moveSpeed);
}
//x축으로 moveSpeed만큼 매 프레임마다 이동 (World: 글로벌 좌표계)
this.transform.Translate(0, this.moveSpeed, 0,Space.World);
//감쇠 매프레임마다 0.95f를 moveSpeed에 곱해서 적용 -> 점점 줄이겠다 -> 0.00xx 0과 비슷해짐
this.moveSpeed *= this.dampingCoefficient;
// Z축을 중심으로 rotationSpeed만큼 매 프레임마다 회전 (World: 글로벌 좌표계)
this.transform.Rotate(0, 0, this.rotationSpeed * Time.deltaTime, Space.World);
}
}
반응형
'KDT > 유니티 기초' 카테고리의 다른 글
[Pixel Sword] 캐릭터 동작과 애니메이션 구현 + 캐릭터 위의 땅 충돌시 오류 수정 (0) | 2023.08.04 |
---|---|
[ClimbCloud] 제작 (0) | 2023.08.02 |
[CatEscape] Arrow 프리팹 + HP +GameOver + Score 구현 (0) | 2023.08.02 |
[CatEscape] 화살표와 고양이 충돌 판정 (0) | 2023.08.01 |
[SwipeCar] SwipeCar 만들기 + 오디오 출력 (0) | 2023.08.01 |