Yang.공부방

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