您的位置:首页 > 移动开发 > Unity3D

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()

Objectives

ThemainobjectiveofthisblogpostistoexplainhowtorotateanygameObjectsmoothlywithoutanyanimation.SmoothRotationofObjectTutorial

Step1ProjectSetup

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.
Direction1forrightdirection
Direction-1forleftdirection

2.3voidCheckForVerticalInput()

-Iftheobjectisnotrotatingcurrently,itwillgetaverticalinputfromtheuser(Up/Down).
-Setverticalflagtotrue.
-Accordingtotheinput,setdirectionforrotation.
Direction1forupdirection
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.
GotanIdeaofGameDevelopment?Whatareyoustillwaitingfor?Contact
usnowandseetheIdealivesoon.OurcompanyhasbeennamedasoneofthebestGameDevelopmentCompanyinIndia.

-Seemoreat:http://www.theappguruz.com/blog/smooth-rotation-of-object-unity#sthash.ad8QDA7A.dpuf
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: