Smooth Rotation of Object in UNITY
2016-05-19 23:21
459 查看
form:http://www.theappguruz.com/blog/smooth-rotation-of-object-unity
JumpTo:
ProjectSetup
CodeSample
SmoothRotationScript
voidCheckForHorizontalInput()
voidCheckForVerticalInput()
voidHorizontalRotation()
voidVerticalRotation()
voidUpdate()
Setanangleatwhichyouwanttorotatetheobject.
[/code]
-Sethorizontalflagtotrue.
-Accordingtotheinput,setdirectionforrotation.
-Setverticalflagtotrue.
-Accordingtotheinput,setdirectionforrotation.
-Oncetheobjectissettotargetrotation,itwillstoprotatingandwillsetisRotatingtofalse.
SettempAngleto0.
SethorizontalFlagtofalse.
-Oncetheobjectissettotargetrotation,itwillstoprotatingandwillsetisRotatingtofalse.
SettempAngleto0.
SetverticalFlagtofalse.
-Continuouscheckforverticalinput.
-IfhorizontalFlagbecomestrue,callHorizontalRotation().
-IfverticalFlagbecomestrue,callVerticalRotation().
IhopeyoufindthisblogisveryhelpfulwhileworkingwithSmoothRotationobjectinUnity.LetmeknowincommentifyouhaveanyquestionregardingSmoothRotation
Object.
GotanIdeaofGameDevelopment?Whatareyoustillwaitingfor?Contact
usnowandseetheIdealivesoon.OurcompanyhasbeennamedasoneofthebestGameDevelopmentCompanyinIndia.
-Seemoreat:http://www.theappguruz.com/blog/smooth-rotation-of-object-unity#sthash.ad8QDA7A.dpuf
JumpTo:
Objectives
ThemainobjectiveofthisblogpostistoexplainhowtorotateanygameObjectsmoothlywithoutanyanimation.SmoothRotationofObjectTutorialStep1ProjectSetup
Addagameobjectinthehierarchythatyouwanttorotate.AddSmoothRotationscripttothatobject.Setanangleatwhichyouwanttorotatetheobject.
Step2CodeSample
2.1SmoothRotationScript
usingUnityEngine;
usingSystem.Collections;
publicclassSmoothRotation:MonoBehaviour{
[Range(0,360)]publicfloatangle;//SpecifyAngleForRotation
floattempAngle;//TemporaryAngleMeasurementVariable
boolhorizontalFlag;//CheckForHorizontalRoation
boolverticalFlag;//CheckForVerticalRoation
boolisRotating;//CheckWhetherCurrentlyObjectisRotatingOrNot.
intDirection;//DirectionOfRotation
//CalledForInitialization
voidStart(){
horizontalFlag=verticalFlag=isRotating=false;
}
//MethodForHorizontalInput
voidCheckForHorizontalInput()
{
if(Input.GetAxis("Horizontal")!=0&&!isRotating)
{
isRotating=true;
Direction=(Input.GetAxis("Horizontal")<0?-1:1);
horizontalFlag=true;
tempAngle=0;
}
}
//MethodForVerticalInput
voidCheckForVerticalInput()
{
if(Input.GetAxis("Vertical")!=0&&!isRotating)
{
isRotating=true;
Direction=(Input.GetAxis("Vertical")<0?-1:1);
verticalFlag=true;
tempAngle=0;
}
}
//MethodForhorizontalRotation
voidHorizontalRotation()
{
transform.Rotate(Vector3.up*angle*Time.fixedDeltaTime*Direction,Space.World);
tempAngle+=angle*Time.fixedDeltaTime;
if(tempAngle>=angle)
{
tempAngle=0;
isRotating=false;
horizontalFlag=false;
}
}
//MethodForVerticalRotation
voidVerticalRotation()
{
transform.Rotate(Vector3.right*angle*Time.fixedDeltaTime*Direction,Space.World);
tempAngle+=angle*Time.fixedDeltaTime;
if(tempAngle>=angle)
{
tempAngle=0;
isRotating=false;
verticalFlag=false;
}
}
voidUpdate(){
CheckForHorizontalInput();
CheckForVerticalInput();
if(horizontalFlag)
HorizontalRotation();
if(verticalFlag)
VerticalRotation();
}
}
[/code]
2.2voidCheckForHorizontalInput()
-Iftheobjectisnotrotatingcurrently,itwillgetahorizontalinputfromtheuser(Left/Right).-Sethorizontalflagtotrue.
-Accordingtotheinput,setdirectionforrotation.
Direction | 1forrightdirection |
Direction | -1forleftdirection |
2.3voidCheckForVerticalInput()
-Iftheobjectisnotrotatingcurrently,itwillgetaverticalinputfromtheuser(Up/Down).-Setverticalflagtotrue.
-Accordingtotheinput,setdirectionforrotation.
Direction | 1forupdirection |
Direction | -1fordowndirection |
2.4voidHorizontalRotation()
-ItwillrotatetheobjectaccordingtothedirectionuntiltempAngleislessthenangle.-Oncetheobjectissettotargetrotation,itwillstoprotatingandwillsetisRotatingtofalse.
SettempAngleto0.
SethorizontalFlagtofalse.
2.5voidVerticalRotation()
-ItwillrotatetheobjectaccordingtothedirectionuntiltempAngleislessthenangle.-Oncetheobjectissettotargetrotation,itwillstoprotatingandwillsetisRotatingtofalse.
SettempAngleto0.
SetverticalFlagtofalse.
2.6voidUpdate()
-Continuouscheckforhorizontalinput.-Continuouscheckforverticalinput.
-IfhorizontalFlagbecomestrue,callHorizontalRotation().
-IfverticalFlagbecomestrue,callVerticalRotation().
IhopeyoufindthisblogisveryhelpfulwhileworkingwithSmoothRotationobjectinUnity.LetmeknowincommentifyouhaveanyquestionregardingSmoothRotation
Object.
GotanIdeaof
usnowandseetheIdealivesoon.OurcompanyhasbeennamedasoneofthebestGameDevelopmentCompanyinIndia.
-Seemoreat:
相关文章推荐
- Unity3D着色器Shader
- Unity优化:从Draw Calls到GC
- Unity3D世界的坐标系
- unity3d项目文件目录发布后,对应的ios/android应用目录
- unity烘培
- unity 合并skinnedMeshRenderer中遇到的一个大坑
- Unity编辑器扩展-菜单项
- Unity 里双击脚本打不开的解决方法
- 如何将资源打包成unitypackage的形式
- Unity之Invoke、InvokeRepeating、StartCoroutine学习一
- Unity3D之VR开发笔记@Oculus开发UNITY环境搭建
- Unity3d 顶点着色器传入的顶点数据
- (转载)Unity3D研究院之处理摄像机与角色之间被挡时的局部透明效果
- (转载)Unity3D研究院之获取摄像机的视口区域
- Unity4.3 bug GetChild顺序错乱
- Unity对象的所有组件深拷贝与粘贴
- Unity对象的所有组件深拷贝与粘贴
- Unity3D之VR开发笔记@大朋VR开发UNITY环境搭建
- [UnityUI]UGUI按钮长按效果
- Unity中的C#内存管理(三)