[Unity자습서] Background이동
Unity/수업내용2019. 5. 7. 18:36
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 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 | using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; public class TestSunnyLand : MonoBehaviour { public Button idleBtn; public Button runBtn; public Button jumpBtn; public float backgroundSpeed; public float groundSpeed; public float treeSpeed; public Transform background; public Transform ground; public Transform[] arrTree; Vector3 backGroundPosition; Vector3 groundPosition; private float backGroundSize = 3.65f; private float treeSize = 7f; private bool isMove = false; private void Start() { this.backGroundPosition = this.background.transform.position; this.groundPosition = this.ground.transform.position; } void Update() { var anim = GetComponent<Animator>(); idleBtn.onClick.AddListener(() => { this.isMove = false; anim.Play("player_idle"); }); runBtn.onClick.AddListener(() => { this.isMove = true; anim.Play("player_run"); //StartCoroutine(this.AngleMove()); }); jumpBtn.onClick.AddListener(() => { this.isMove = true; anim.Play("player_jump"); }); } private void FixedUpdate() { if (this.isMove == true) { var newPosition = Mathf.Repeat(Time.time * backgroundSpeed, backGroundSize); var treeNewPosition = Mathf.Repeat(Time.time * backgroundSpeed, backGroundSize); this.background.transform.position = this.backGroundPosition + Vector3.right * newPosition; this.ground.transform.position = this.groundPosition + Vector3.left * newPosition; for (int i = 0; i < arrTree.Length; i++) { var treeTrans = this.arrTree[i]; this.arrTree[i].Translate(Vector3.left * treeSpeed * Time.deltaTime); if (treeTrans.transform.localPosition.x <= -1f) { treeTrans.transform.localPosition = new Vector3(4.6f, -0.17f, 0); } } } } } | cs |
'Unity > 수업내용' 카테고리의 다른 글
isometric (0) | 2019.05.12 |
---|---|
[Unity자습서] 점프 및 보석 획득 (0) | 2019.05.12 |
[Unity자습서] Space Shoot 해부 (WASD키로 이동),(화면밖으로 못나가게 하기) (0) | 2019.05.02 |
[Unity자습서] Space Shoot 해부 (배경) (0) | 2019.05.02 |
Move,Rotation,Shooting (0) | 2019.05.01 |