博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
unity的旋转
阅读量:4653 次
发布时间:2019-06-09

本文共 5777 字,大约阅读时间需要 19 分钟。

 

绕着一个点旋转 :

transform.RotateAround(Vector3.zero, Vector3.up, speed* Time.deltaTime );

第一个参数,点的位置。第二个参数,法线方向,第三个参数,速度.如图时针旋转。

 

 


旋转固定角度

gameObject.transform.rotation = Quaternion.Slerp(gameObject.transform.rotation, Quaternion.Euler(60,0, 0), 6);

第一个参数起始角度,第二参数结束角度,第三个参数旋转工消耗的时间。


 

static function LookRotation (forward : , upwards :  = ) : Quaternion

创建一个旋转,沿着forward(z轴)并且头部沿着upwards(y轴)的约束注视。也就是建立一个旋转,使z轴朝向view  y轴朝向up。

 

public Transform target;    void Update() {        Vector3 relativePos = target.position - transform.position;        Quaternion rotation = Quaternion.LookRotation(relativePos);        transform.rotation = rotation;    }

 

WSAD旋转,阻尼旋转

    float smooth = 2.0f;

    float tiltAngle = 30.0f;

   void Update()    {        float tiltAroundZ = Input.GetAxis("Horizontal") * tiltAngle;        float tiltAroundX = Input.GetAxis("Vertical") * tiltAngle;        var target = Quaternion.Euler(tiltAroundX, 0, tiltAroundZ);        // Dampen towards the target rotation        //向target旋转阻尼        transform.rotation = Quaternion.Slerp(transform.rotation, target,        Time.deltaTime * smooth);     }

鼠标点着旋转1:

private Transform hitTransfrom;   void Update(){    if (Input.GetMouseButtonDown(0))    {        RaycastHit hit;        Ray mouseray = Camera.main.ScreenPointToRay(Input.mousePosition);        if (Physics.Raycast(mouseray, out hit))        {            hitTransfrom = hit.transform;        }    }    else if (Input.GetMouseButtonUp(0))    {        hitTransfrom = null;    }    if (hitTransfrom)    {        Matrix4x4 localmatrix = hitTransfrom.worldToLocalMatrix;        Vector3 vUp = localmatrix.MultiplyVector(new Vector3(0, 1, 0));        Vector3 vRight = -localmatrix.MultiplyVector(new Vector3(1, 0, 0));        float fMoveX = -Input.GetAxis("Mouse X") * Time.deltaTime * 200.0f;        Quaternion rotation = Quaternion.AngleAxis(fMoveX, vUp);        hitTransfrom.localRotation *= rotation;        float fMoveY = -Input.GetAxis("Mouse Y") * Time.deltaTime * 200.0f;        Quaternion rotoy = Quaternion.AngleAxis(fMoveY, vRight);        hitTransfrom.localRotation *= rotoy;    } }

鼠标点着旋转2:

void Update()    {        if (Input.GetMouseButtonDown(0))        {            OnMouseDrag();        }    }    void OnMouseDrag()    {        this.transform.Rotate(new Vector3(Input.GetAxis("Mouse Y"), -Input.GetAxis("Mouse X"), 0) * 6f, Space.World);    }

 缩放的代码,缩放、自动旋转、拖拽物体

private bool onDrag = false;    public float speed = 6f;    private float tempSpeed;    private float axisX;    private float axisY;    private float cXY;          void OnMouseDown(){        axisX=0f;        axisY=0f;    }    void OnMouseDrag ()    {        onDrag = true;        axisX = -Input.GetAxis ("Mouse X");        axisY = Input.GetAxis ("Mouse Y");        cXY = Mathf.Sqrt (axisX * axisX + axisY * axisY);        if(cXY == 0f){            cXY=1f;        }    }          float Rigid ()    {        if (onDrag) {            tempSpeed = speed;        } else {            if (tempSpeed > 0) {                tempSpeed -= speed*2 * Time.deltaTime / cXY;            } else {                tempSpeed = 0;            }        }        return tempSpeed;    }          void Update ()    {        gameObject.transform.Rotate (new Vector3 (axisY, axisX, 0) * Rigid (), Space.World);        if (!Input.GetMouseButton (0)) {            onDrag = false;        }    }

 

using UnityEngine;using System.Collections;  /**   * @Function:使物体自动旋转,鼠标能够360°拖转物体,托转物体的时候,自动旋转停止,同时滚轮实现物体的缩放功能 * @Ahthor:黄杰 * @Date:2013-04-24   */public class ZoomAndDrag : MonoBehaviour {      public Camera MainCamera;    public float ZoomMin;      //滚轮的最小值    public  float ZoomMax;      //滚轮的最大值    private float normalDistance;   //设置摄像机的景深值    private float MouseWheelSencitivity = 10.0f;    //鼠标灵敏度,就是缩放的速度的快慢     private float axisX;    private float axisY;    public float speed = 6f;    private float tempSpeed;     private bool RoationOnly;     void Start ()     {        normalDistance = 50.0f;        ZoomMin = 20.0f;        ZoomMax = 100.0f;        RoationOnly = true;    }              void Update ()     {        Roation();        Zoom();        this.transform.Rotate(new Vector3(axisY, axisX, 0) * Rigid(), Space.World);     //物体旋转的方法    }      //自动旋转物体的方法,放在Update中调用    void Roation()    {        if (RoationOnly)        {            gameObject.transform.Rotate(Vector3.up * Time.deltaTime * 10);        }    }     /****    *鼠标滚轮缩放物体的方法     *      * **/    void Zoom()    {        if (Input.GetAxis("Mouse ScrollWheel") != 0)        {            if (normalDistance >= ZoomMin && normalDistance <= ZoomMax)            {                normalDistance -= Input.GetAxis("Mouse ScrollWheel") * MouseWheelSencitivity;            }            if (normalDistance < ZoomMin)            {                normalDistance = ZoomMin;            }            if (normalDistance > ZoomMax)            {                normalDistance = ZoomMax;            }             MainCamera.fieldOfView = normalDistance;        }    }  /***   *    * 鼠标左键控制物体360°旋转+惯性   * **/    float Rigid()    {        if (Input.GetMouseButton(0))        {            RoationOnly = false;    //当鼠标按下的时候,停止自动旋转             axisX = Input.GetAxis("Mouse X");            axisY = Input.GetAxis("Mouse Y");            if (tempSpeed < speed)            {                tempSpeed += speed * Time.deltaTime * 5;            }            else            {                tempSpeed = speed;            }        }        else        {            RoationOnly = true;     //当鼠标没有按下的时候,恢复自动旋转            if (tempSpeed > 0)            {                tempSpeed -= speed * Time.deltaTime;            }            else            {                tempSpeed = 0;            }        }        return tempSpeed;    }}

 

转载于:https://www.cnblogs.com/martianzone/p/3323558.html

你可能感兴趣的文章
Solution for Concurrent number of AOS' for this application exceeds the licensed number
查看>>
CSE 3100 Systems Programming
查看>>
IntelliJ IDEA 的Project structure说明
查看>>
Java Security(JCE基本概念)
查看>>
Linux Supervisor的安装与使用入门
查看>>
创建 PSO
查看>>
JasperReport报表设计4
查看>>
项目活动定义 概述
查看>>
团队冲刺04
查看>>
我的Python分析成长之路8
查看>>
泛型在三层中的应用
查看>>
SharePoint2010 -- 管理配置文件同步
查看>>
.Net MVC3中取得当前区域的名字(Area name)
查看>>
获得屏幕像素以及像素密度
查看>>
int与string转换
查看>>
adb命令 判断锁屏
查看>>
推荐一个MacOS苹果电脑系统解压缩软件
查看>>
1035等差数列末项计算
查看>>
CDMA鉴权
查看>>
ASP.NET MVC Identity 兩個多個連接字符串問題解決一例
查看>>