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 ...
随机推荐
- 如何把 excel 设为文本格式?
选择要设置的单元格,右键选择 --- “设置单元格格式” --- 选 “ 分类 ” 下面的 “ 文本 ” --- 确定. 修改前: 修改后:
- MS OFFICE 2010破解版安装
受人所托发布一个MS OFFICE 2010破解版的傻瓜安装教程,刚好新本本也没有安装,安装过程中截了几个图就搞定了. 安装包: http://www.itopdog.cn/soft/office20 ...
- C# 之 遍历本地文件夹下的所有文件
/// <summary> /// 遍历 rootdir目录下的所有文件 /// </summary> /// <param name="rootdir&quo ...
- C# 之 Math取整
主要用到 System 命名空间下的一个数据类 Math ,调用他的方法. C#取整函数使用详解: 1.Math.Round是"就近舍入",当要舍入的是5时与"四舍五入& ...
- 使用webView制作浏览器
xml文件: <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:t ...
- C#实现插入排序法
private int[] test_Insertion(int[] list) //插入排序 传入数组 3, 6, 2, 1, 9, 5, 4, 7 { ;i<list.Length;i++) ...
- nvl()函数
Oracle里面的nvl()函数 Nvl(字段名,0):如果你某个字段为空,但是你想让这个字段显示0,nvl(字段名,0) ,就是当你选择出来的时候,这个字段虽然为空,但是显示的是0,当然这个0也可以 ...
- 使用VERT.X构建分布式企业级应用
谈到企业应用,就得谈分布式.低耦合.模块化.面向服务.可扩展性等等.早些时候的技术有CORBA和EJB,后面兴起的有WebService和MDB.但是这些技术不是学习.开发门槛高就是不那么轻量化.我现 ...
- Java_Web _Servlet生命周期实验
第一次加载这个servlet程序时(选择右边的servlet程序,注意servlet程序没有main函数,因此执行的是run as servlet Application ),同时执行init()方法 ...
- C#常量字段
const 常量字段使用方法 using System;using System.Collections.Generic;using System.Linq;using System.Text;usi ...