본문 바로가기
Programming/C# * Unity

[Unity] 02. 이동(Transform)

by 고막고막 2019. 2. 27.


1. Move

  • scale을 키우면 이동시마다 연산하기 때문에 성능이 떨어짐 → scale factor로 자체를 키운다.
  • 부모-자식 설정할때의 위치,각도는 부모객체의 좌표를 기준으로 하는 상대좌표이다.
1
2
3
4
5
6
7
8
9
10
11
12
        // 키보드 입력
        float h = Input.GetAxis("Horizontal");
        float v = Input.GetAxis("Vertical");
 
        // 이동 거리 보정 : 디바이스에 따라 다른 프레임 호출 횟수에 영향을 받지 않기 위해 보정
        // -> 성능 좋은 디바이스의 경우 같은 시간에 촘촘하게 움직이므로 매끄럽게 보여진다.
        h = h * Time.deltaTime * speed;
        v = v * Time.deltaTime * speed;
 
        // 이동
        this.transform.Translate(Vector3.right * h);
        this.transform.Translate(Vector3.right * v);
cs


2. Rotate

1
2
3
4
5
6
    public float rotSpeed = 50f;
 
    void Update()
    {
        transform.Rotate(Vector3.up * rotSpeed * Time.deltaTime);    
    }
cs

3. Throw

  • Add Force("내 손을 떠났다!") - mass에 영향을 받음
  • Velocity(중간 제어 가능) - mass에 영향을 받지 않음
1
2
3
4
5
6
7
8
9
10
11
12
13
14
 public float power = 300f;
    public Vector3 velocity = new Vector3(0, 1f, 1f);
    // velocity: y,z축 45도 방향 설정
 
    // 딜레이 없이 고정된 속도로 호출한다는 약속
    void FixedUpdate()
    {
        if (Input.GetButtonDown("fire1"))   // 마우스 왼쪽 버튼
        {
            velocity = velocity * power;
            this.GetComponent<Rigidbody>().AddForce(velocity);
            velocity = new Vector3(0, 1f, 1f);  // 초기화
        }
    }
cs



'Programming > C# * Unity' 카테고리의 다른 글

[C#] while문과 do while문 비교  (3) 2019.03.05
[Unity] 03. 지형(Terrain)  (0) 2019.03.04
[C#] switch~case문 응용  (0) 2019.03.04
[Unity] 01. 재질(Material)  (0) 2019.02.27
[Unity] 00. 기본 컨트롤  (0) 2019.02.26