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 ...
随机推荐
- Encryption
Encryption Configuration Basic Usage Encrypting a value Decrypting a value Configuration Before usin ...
- Android Studio编译FBReaderJ
我的个人环境 系统:mac (windows应该差不多) 工具:android studio 2.1.2 注意:一定要安装NDK!一定要安装NDK!一定要安装NDK! 如何安装NDK ...
- div+css(ul li)实现图片上文字下列表布局
css样式表代码: html布局代码: 效果图: html布局部分,可根据自己需要添加对应的div即可. 1.CSS关键样式单词解释 1).ul.imglist{ margin:0 auto; wid ...
- 纯CSS实现三列布局(两边固定,中间自适应)
看了一些网上的案例,感觉较繁杂,于是,自己整理了一篇来说明这个东西. 也是给我自己复习吧,以前有人问道,我还没答上来呢.== 看代码: html: <div class="top&qu ...
- dedecms安装步骤
GD支持:PHP设置-PHP扩展-php_gd2 初始化数据体验包:点击下载:或者点击取消 如果是本地安装在数据库的在数据库用户名选择默认的(root),密码为空 主要是如果是基于远程服务器的 ...
- ThinkPHP的配置
ThinkPHP配置:conf目录下 'URL_PATHINFO_DEPR'=>'-',//修改URL的分隔符, 'TMPL_L_DELIM'=>'<{', //修改左定界符 'TM ...
- Android之Http网络编程(三)
在前面两篇博客<Android之Http网络编程(一)>.<Android之Http网络编程(二)>中,简单的介绍了对网页的请求和客户端与服务端的简单的参数交互.那么,这一篇博 ...
- 转-----EasyCHM制作教程
希望以后自己的笔记能够整理成 chm 格式的文档 制作过CHM帮助文件的同志们可能都遇到过以下两个问题: 1.制作好的CHM文件图像.公式不显示. 2.制作好的CHM文件在自己电脑上能显示,在别人电脑 ...
- sql 更新重复数据只取一条记录
select s.* from ( select *, row_number() over (partition by PersonnelAccount order BY Personnel ...
- WCF编程系列(一)初识WCF
WCF编程系列(一)初识WCF Windows Communication Foundation(WCF)是微软为构建面向服务的应用程序所提供的统一编程模型.WCF的基本概念: 地址:定义服务的 ...