Unity2014. 8. 10. 16:35

고정된 카메라가 앞에 놓여 있는 물체 움직임을 추적하며 상하좌우로 회전하게 하려면

http://docs.unity3d.com/ScriptReference/Transform-rotation.html


using UnityEngine;
using System.Collections;

public class ExampleClass : MonoBehaviour {
    public float smooth = 2.0F;
    public float tiltAngle = 30.0F;
    void Update() {
        float tiltAroundZ = Input.GetAxis("Horizontal") * tiltAngle;
        float tiltAroundX = Input.GetAxis("Vertical") * tiltAngle;
        Quaternion target = Quaternion.Euler(tiltAroundX, 0, tiltAroundZ);
        transform.rotation = Quaternion.Slerp(transform.rotation, target, Time.deltaTime * smooth);
    }
}


예제 코드를 테스트하지 않고 수정하여 사용했습니다.

using UnityEngine; using System.Collections; public class OverWatch : MonoBehaviour { public Transform targetCar; private Vector3 offset; public float smooth = 2.0F; public float tiltAngle = 30.0F; // Use this for initialization void Start () { //offset = transform.position; offset.x = this.targetCar.position.x; offset.y = this.targetCar.position.y; } // Update is called once per frame void Update () { //transform.position = target.position + offset; float tiltAroundY = (offset.x - this.targetCar.position.x) * tiltAngle; // ㄱ float tiltAroundX = (offset.y - this.targetCar.position.y) * tiltAngle; // ㄴ Quaternion target = Quaternion.Euler(tiltAroundX, tiltAroundY, 0); // ㄷ transform.rotation = Quaternion.Slerp(transform.rotation, target, Time.deltaTime * smooth); //transform.rotation *= target; offset.x = this.targetCar.position.x; offset.y = this.targetCar.position.y; // ㅁ } }




원하는 방향으로 회전하는 결과를 얻을 수 없었습니다. 하는 중에  LookAt함수를 찾았고 ㄱ~ㅁ까지 한 줄로 바꿀 수 있었습니다.

transform.LookAt (this.targetCar.position);


'Unity' 카테고리의 다른 글

유니티 2D 스프라이트  (0) 2014.08.07
Animator 애니메이터 (매카님) 에서 지원하는 파라미터  (0) 2014.08.07
Ray 쏘아 오브젝트 삭제  (0) 2014.08.06
유니티 물체 배치  (0) 2014.08.06
Trail Renderer  (0) 2014.08.06
Posted by 코드버무려