[Unity자습서] Space Shoot 해부 (배경)
Unity/수업내용2019. 5. 2. 01:57
Mathf.Repeat(float t, float length);
: t를 0부터 length안의 범위로 반복시키는 메서드(t는 0보다 크고 length보다 작아야한다)
ex) Mathf.Repeat(3.0, 2.5) 의 결과 값 : 0.5
최소값 : 0, 최대값 : length
무조건 양수여야 하고 제한된 값만큼만 나오게 할 때 사용한다.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 | using System.Collections; using System.Collections.Generic; using UnityEngine; public class TestBGScroller : MonoBehaviour { public Transform[] arrGround; public float speed = 5f; private float tileSize = 30; Vector3 movePosition; //수업중에 한것(단점 : new Vector3 하는 동안에도 이동중이므로 틈이 생긴다 private void Update() { for (int i = 0; i < this.arrGround.Length; i++) { var groundTrans = this.arrGround[i]; this.arrGround[i].Translate(-Vector3.up * speed * Time.deltaTime); //두장일때 //if (groundTrans.transform.localPosition.z <= -29.5) //{ // groundTrans.transform.localPosition = new Vector3(0, 0, 30); //} //세장일때 if (groundTrans.transform.localPosition.z <= -29) { groundTrans.transform.localPosition = new Vector3(0, 0, 60); } } } //여기부터 자습서에서 따온것(Repeat 사용) private void Start() { this.movePosition = this.transform.position; } private void FixedUpdate() { var newPosition = Mathf.Repeat(Time.time * speed, tileSize); this.transform.position = this.movePosition - Vector3.forward * newPosition; } } | cs |
참조 : https://dawninthemoon.tistory.com/12
API : https://docs.unity3d.com/kr/530/ScriptReference/Mathf.Repeat.html
'Unity > 수업내용' 카테고리의 다른 글
[Unity자습서] Background이동 (0) | 2019.05.07 |
---|---|
[Unity자습서] Space Shoot 해부 (WASD키로 이동),(화면밖으로 못나가게 하기) (0) | 2019.05.02 |
Move,Rotation,Shooting (0) | 2019.05.01 |
조이스틱으로 이동하기 및 맵밖으로 이동시키지 않기 (0) | 2019.04.26 |
버튼 누르면 공격 (반복) 및 데미지Text (0) | 2019.04.25 |