https://docs.unity3d.com/kr/530/ScriptReference/Mathf.Clamp.html
Mathf-Clamp - Unity 스크립팅 API
Clamps a value between a minimum float and maximum float value.
docs.unity3d.com
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class GameBoard : MonoBehaviour
{
public float maxDegree = 45f;
public float rotateSpeed = 3;
private float curDegree;
private void Awake()
{
curDegree = transform.eulerAngles.z;
}
public void RotateCW()
{
curDegree -= rotateSpeed * Time.deltaTime;
curDegree = Mathf.Clamp(curDegree, -maxDegree, maxDegree);
transform.eulerAngles = new Vector3(0, 0, curDegree);
}
public void RotateCCW()
{
curDegree += rotateSpeed * Time.deltaTime;
curDegree = Mathf.Clamp(curDegree, -maxDegree, maxDegree);
transform.eulerAngles = new Vector3(0, 0, curDegree);
}
}
-> 45도 ~ -45도 사이의 회전
회전할 오브젝트 안에 회전 함수 만들어 넣어놓고 다른 스크립트에서 일정 조건 시 불러와서 사용
'Unity' 카테고리의 다른 글
(Unity) 유니티 Scripts 라이프 사이클(생명주기,Life Cycle) (0) | 2022.12.16 |
---|---|
(Unity) Script로 오브젝트 생성하기 - CreatePrimitive (0) | 2022.12.07 |
(Unity) BoxCollider 코드로 켜고 끄기 (0) | 2022.12.06 |
(Unity) JSON을 통한 데이터 처리 in Unity (0) | 2022.10.24 |
(Unity) 코루틴, Couroutine (0) | 2022.10.12 |