unity的旋转
绕着一个点旋转 :
- transform.RotateAround(Vector3.zero, Vector3.up, speed* Time.deltaTime );
第一个参数,点的位置。第二个参数,法线方向,第三个参数,速度.如图时针旋转。
旋转固定角度
- gameObject.transform.rotation = Quaternion.Slerp(gameObject.transform.rotation, Quaternion.Euler(,, ), );
第一个参数起始角度,第二参数结束角度,第三个参数旋转工消耗的时间。
static function LookRotation (forward : Vector3, upwards : Vector3 = Vector3.up) : 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, , 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())
- {
- RaycastHit hit;
- Ray mouseray = Camera.main.ScreenPointToRay(Input.mousePosition);
- if (Physics.Raycast(mouseray, out hit))
- {
- hitTransfrom = hit.transform;
- }
- }
- else if (Input.GetMouseButtonUp())
- {
- hitTransfrom = null;
- }
- if (hitTransfrom)
- {
- Matrix4x4 localmatrix = hitTransfrom.worldToLocalMatrix;
- Vector3 vUp = localmatrix.MultiplyVector(new Vector3(, , ));
- Vector3 vRight = -localmatrix.MultiplyVector(new Vector3(, , ));
- 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())
- {
- OnMouseDrag();
- }
- }
- void OnMouseDrag()
- {
- this.transform.Rotate(new Vector3(Input.GetAxis("Mouse Y"), -Input.GetAxis("Mouse X"), ) * 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 > ) {
- tempSpeed -= speed* * Time.deltaTime / cXY;
- } else {
- tempSpeed = ;
- }
- }
- return tempSpeed;
- }
- void Update ()
- {
- gameObject.transform.Rotate (new Vector3 (axisY, axisX, ) * Rigid (), Space.World);
- if (!Input.GetMouseButton ()) {
- 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, ) * Rigid(), Space.World); //物体旋转的方法
- }
- //自动旋转物体的方法,放在Update中调用
- void Roation()
- {
- if (RoationOnly)
- {
- gameObject.transform.Rotate(Vector3.up * Time.deltaTime * );
- }
- }
- /****
- *鼠标滚轮缩放物体的方法
- *
- * **/
- void Zoom()
- {
- if (Input.GetAxis("Mouse ScrollWheel") != )
- {
- 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())
- {
- RoationOnly = false; //当鼠标按下的时候,停止自动旋转
- axisX = Input.GetAxis("Mouse X");
- axisY = Input.GetAxis("Mouse Y");
- if (tempSpeed < speed)
- {
- tempSpeed += speed * Time.deltaTime * ;
- }
- else
- {
- tempSpeed = speed;
- }
- }
- else
- {
- RoationOnly = true; //当鼠标没有按下的时候,恢复自动旋转
- if (tempSpeed > )
- {
- tempSpeed -= speed * Time.deltaTime;
- }
- else
- {
- tempSpeed = ;
- }
- }
- return tempSpeed;
- }
- }
unity的旋转的更多相关文章
- [转]Unity 3D旋转矢量方向及二维平面基于一点选择另一点(Rotate a Vector3 direction & Rotate a point about another point in 2D )
http://specialwolf.blog.163.com/blog/static/124466832201301332432766/ ****************************** ...
- unity 对象旋转,自转
1.对象具体的围绕哪个轴旋转,对应的设置值: transform.Rotate(new Vector3(1,0,0)); //绕x轴旋转 //默认是物体围绕世界坐标的XYZ轴旋转,即物体绕着世 ...
- Unity 摄像机旋转初探
接触打飞机的游戏时都会碰见把摄像机绕 x 轴顺时针旋转 90°形成俯瞰的视角的去看飞船.也没有多想,就感觉是坐标系绕 x 轴旋转 90°完事了.但是昨天用手比划发一下发现不对.我就想这样的话绕 x 轴 ...
- Blender模型导入进Unity,旋转缩放的调整
Blender跟Unity的XYZ轴不同的原因,导致Blender模型导入Unity之后会发生模型朝向不对. 请先看看下边这个情况: 首先,Blender物体模式下,对模型进行 旋转 缩放,将会在右边 ...
- Unity的旋转-四元数,欧拉角用法简介
当初弄不明白旋转..居然找不到资料四元数应该用轴角相乘...后来自己摸明白了 通过两种旋转的配合,可以告别世界空间和本地空间矩阵转换了,大大提升效率. 每个轴相乘即可,可以任意轴,无限乘.无万向节锁问 ...
- Unity 物体旋转会发生变形
当游戏对象的 "父物体们" 有一个是缩放的,也就是Scale不是(1,1,1)的时候,旋转这个游戏对象它就会出现变形的现象.
- unity 角色旋转
using UnityEngine; using System.Collections; public class Triangle : MonoBehaviour { public float sp ...
- Unity 逐步旋转
npc.transform.rotation = Quaternion.Slerp(npc.transform.rotation, Quaternion.LookRotation(moveDir), ...
- Unity 360 旋转 缩放
using UnityEngine; using System.Collections; public class SandR : MonoBehaviour { public GameObject ...
随机推荐
- What Influences Method Call Performance in Java?--reference
reference from:https://www.voxxed.com/blog/2015/02/too-fast-too-megamorphic-what-influences-method-c ...
- Regular Expressions --正则表达式官方教程
http://docs.oracle.com/javase/tutorial/essential/regex/index.html This lesson explains how to use th ...
- linux crt
1.仿真 终端选linux ANSI颜色[有颜色了] 使用颜色方案[颜色加深了] 2.外观 选传统的 ,utf-8 就不会乱码了
- View绘制详解(四),谝一谝layout过程
上篇博客我们介绍了View的测量过程,这只是View显示过程的第一步,第二步就是layout了,这个我们一般译作布局,其实就是在View测量完成之后根据View的大小,将其一个一个摆放在ViewGro ...
- Javascript之基本包装类型
一.基本包装类型概述 var box = 'Mr. Lee';//定义一个字符串 var box2 = box.substring(2);//截掉字符串前两位 alert(box2);//输出新字符串 ...
- JAVA_JSON_example
package cn.kjxy.JSON; import java.util.List; import org.json.JSONArray; import org.json.JSONExceptio ...
- C#总结1
C#摘要 第一章: 数据类型: 在定义变量的时候,记下规则,开头不能是数字,变量名只能包括 字母 “_” 数字 整型类型 名称 CTS类型 说明 范围 sbyte System.SByte 8位有符号 ...
- 为Photoshop添加右键快捷
打开注册表,开始--->运行--->regedit 找到 HKEY_CLASSES_ROOT <----> *<---->shell 新建项,使用Photosh ...
- oracle添加表字段跟修改表字段属性
添加字段 alter table mid_contactinfo add(status varchar(20),createdate varchar(50),modifydate varchar(50 ...
- sql server 2008 r2 清除数据库日志
USE [master] GO ALTER DATABASE [数据库名] SET RECOVERY SIMPLE WITH NO_WAIT GO ALTER DATABASE [数据库名] SET ...