您的位置:首页 > 移动开发 > Unity3D

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

2015-03-07 14:17 309 查看
第一人称相机观察:

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);
	}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: