[Unity基础]对Rotation的一些理解与实例(二)

第一人称相机观察:

using UnityEngine;
using System.Collections;

//第一人称相机观察
public class CameraObserve : MonoBehaviour {
    
	// Update is called once per frame
	void Update () 
    {
        float mouseX = Input.GetAxis("Mouse X");
        float mouseY = Input.GetAxis("Mouse Y");

        //要么上下观察,要么左右观察
        if (Mathf.Abs(mouseX) > Mathf.Abs(mouseY))
            transform.eulerAngles += new Vector3(0, mouseX, 0);
        else
            transform.eulerAngles += new Vector3(-mouseY, 0, 0);//摄像机绕x轴旋转的方向跟鼠标y移动方向相反
	}
}

物体平滑自转90度:

using UnityEngine;
using System.Collections;

//物体平滑自转90度
public class RotateSelf : MonoBehaviour {

    bool isRotateSelf = false;
    Vector3 targetEuler = Vector3.zero;

	// Update is called once per frame
	void Update () 
    {
        if (Input.GetKeyDown(KeyCode.R))
        {
            isRotateSelf = true;
            targetEuler = transform.eulerAngles + new Vector3(0, 90, 0);
        }
        if (Input.GetKeyDown(KeyCode.T))
            isRotateSelf = false;

        //平滑转90度
        if(isRotateSelf)
            transform.rotation = Quaternion.Lerp(transform.rotation, Quaternion.Euler(targetEuler), Time.deltaTime);
	}
}


你可能感兴趣的:(rotation)