[Unity자습서] Space Shoot 해부 (WASD키로 이동),(화면밖으로 못나가게 하기)
Unity/수업내용2019. 5. 2. 02:08
public static Mathf.Clamp(float value, float min, float max);
최대/최소값 사이의 float값이 value범위 외의 값이 되지 않도록 합니다.
자습서에서는 Class를 생성하여 값을 엔진에서 관리하도록 하였다.
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 | using System.Collections; using System.Collections.Generic; using UnityEngine; //[System.Serializable] //public class MyBoundary //{ // public float xMin, xMax, zMin, zMax; //} public class TestControllship : MonoBehaviour { private float speed = 5f; //public MyBoundary myBoundary; // Start is called before the first frame update void Start() { } private void MoveTest(float h, float v) { var moveMent = new Vector3(h, 0, v); var move = moveMent.normalized * this.speed * Time.deltaTime; transform.Translate(move); //var x = Mathf.Clamp(GetComponent<Rigidbody>().position.x, myBoundary.xMin, myBoundary.xMax); //var z = Mathf.Clamp(GetComponent<Rigidbody>().position.z, myBoundary.zMin, myBoundary.zMax); var x = Mathf.Clamp(GetComponent<Rigidbody>().position.x, -4.5f, 4.5f); var z = Mathf.Clamp(GetComponent<Rigidbody>().position.z, -4.5f, 4.5f); GetComponent<Rigidbody>().position = new Vector3(x, 0, z); } // Update is called once per frame private void FixedUpdate() { var h = Input.GetAxisRaw("Horizontal"); var v = Input.GetAxisRaw("Vertical"); this.MoveTest(h, v); } } | cs |
'Unity > 수업내용' 카테고리의 다른 글
[Unity자습서] 점프 및 보석 획득 (0) | 2019.05.12 |
---|---|
[Unity자습서] Background이동 (0) | 2019.05.07 |
[Unity자습서] Space Shoot 해부 (배경) (0) | 2019.05.02 |
Move,Rotation,Shooting (0) | 2019.05.01 |
조이스틱으로 이동하기 및 맵밖으로 이동시키지 않기 (0) | 2019.04.26 |