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 ...
随机推荐
- SQL 四种连接:内连接、左外连接、右外连接、全连接--转载
原文:http://zwdsmileface.iteye.com/blog/2191730 个人理解 内连接(INNER JOIN)(典型的连接运算,使用像 = 或 <> ...
- oracle 事务简介,锁的概念,java访问数据库注意事项
java链接oracle和连接其他数据库一样有两种方式:1 桥接 jdbc-obdc2 jbdc insert语句一次插入大量数据 insert into table (列1,列2,列3) selec ...
- js解析XML
//在当前页面内追加换行标签和指定的HTML内容function w( html ){ $(document.body).append("<br/>" + htm ...
- android 按两次返回键退出应用
private long mExitTime; @Override public boolean onKeyDown(int keyCode, KeyEvent event) { if (keyCod ...
- JavaScript高级程序设计(第三版)学习笔记22、24、25章
第22章,高级技巧 高级函数 安全的类型检测 typeof会出现无法预知的行为 instanceof在多个全局作用域中并不能正确工作 调用Object原生的toString方法,会返回[Object ...
- JAVA的StringBuffer类(转载整理)____非常重要的一个类,线程安全,不用每次创建一个对象,以及和String的区别
核心部分转载自:http://www.cnblogs.com/springcsc/archive/2009/12/03/1616330.html StringBuffer类和String一样,也用来代 ...
- WCF编程系列(一)初识WCF
WCF编程系列(一)初识WCF Windows Communication Foundation(WCF)是微软为构建面向服务的应用程序所提供的统一编程模型.WCF的基本概念: 地址:定义服务的 ...
- 注意事项: Oracle Not Exists 及 Not In 使用
select value from temp_a awhere a.id between 1 and 100and not exists(select * from temp_b b where a. ...
- Swift 2.0基本语法
内容包括:01变量&常量 02分支 03循环 04字符串 05数组 06字典 07函数 01变量&常量 //: Playground - noun: a place where peo ...
- asp.net 异步处理
#region 异步测试 //委托 public delegate void PrintDelegate(string s); [WebMethod] public string yibu() { / ...