Smooth Rotation of Object in UNITY
来源:互联网 发布:网络安全工程师年薪 编辑:程序博客网 时间:2024/05/22 01:54
form:http://www.theappguruz.com/blog/smooth-rotation-of-object-unity
- Project Setup
- Code Sample
- SmoothRotation Script
- void CheckForHorizontalInput()
- void CheckForVerticalInput()
- void HorizontalRotation()
- void VerticalRotation()
- void Update()
Objectives
The main objective of this blog post is to explain how to rotate any gameObject smoothly without any animation. Smooth Rotation of Object Tutorial
Step 1 Project Setup
Add a game object in the hierarchy that you want to rotate. Add SmoothRotation script to that object.
Set an angle at which you want to rotate the object.
Step 2 Code Sample
2.1 SmoothRotation Script
using UnityEngine;
using System.Collections;
publicclassSmoothRotation : MonoBehaviour {
[Range(0,360)]publicfloat angle;//Specify Angle For Rotation
float tempAngle;//Temporary Angle Measurement Variable
bool horizontalFlag;//Check For Horizontal Roation
bool verticalFlag;//Check For Vertical Roation
bool isRotating;//Check Whether Currently Object is Rotating Or Not.
int Direction;//Direction Of Rotation
//Called For Initialization
void Start () {
horizontalFlag=verticalFlag=isRotating=false;
}
//Method For Horizontal Input
void CheckForHorizontalInput()
{
if(Input.GetAxis("Horizontal")!=0 && !isRotating)
{
isRotating=true;
Direction=(Input.GetAxis("Horizontal")<0?-1:1);
horizontalFlag=true;
tempAngle=0;
}
}
//Method For Vertical Input
void CheckForVerticalInput()
{
if(Input.GetAxis("Vertical")!=0 && !isRotating)
{
isRotating=true;
Direction=(Input.GetAxis("Vertical")<0?-1:1);
verticalFlag=true;
tempAngle=0;
}
}
//Method For horizontal Rotation
void HorizontalRotation()
{
transform.Rotate(Vector3.up*angle*Time.fixedDeltaTime*Direction,Space.World);
tempAngle+=angle*Time.fixedDeltaTime;
if(tempAngle>=angle)
{
tempAngle=0;
isRotating=false;
horizontalFlag=false;
}
}
//Method For Vertical Rotation
void VerticalRotation()
{
transform.Rotate(Vector3.right*angle*Time.fixedDeltaTime*Direction,Space.World);
tempAngle+=angle*Time.fixedDeltaTime;
if(tempAngle>=angle)
{
tempAngle=0;
isRotating=false;
verticalFlag=false;
}
}
void Update () {
CheckForHorizontalInput();
CheckForVerticalInput();
if(horizontalFlag)
HorizontalRotation();
if(verticalFlag)
VerticalRotation();
}
}
2.2 void CheckForHorizontalInput()
- If the object is not rotating currently, it will get a horizontal input from the user (Left/Right).
- Set horizontal flag to true.
- According to the input, set direction for rotation.
Direction1 for right directionDirection-1 for left direction2.3 void CheckForVerticalInput()
- If the object is not rotating currently, it will get a vertical input from the user (Up/Down).
- Set vertical flag to true.
- According to the input, set direction for rotation.
Direction1 for up directionDirection-1 for down direction2.4 void HorizontalRotation()
- It will rotate the object according to the direction until tempAngle is less then angle.
- Once the object is set to target rotation, it will stop rotating and will set isRotating to false.
Set tempAngle to 0.
Set horizontalFlag to false.
2.5 void VerticalRotation()
- It will rotate the object according to the direction until tempAngle is less then angle.
- Once the object is set to target rotation, it will stop rotating and will set isRotating to false.
Set tempAngle to 0.
Set verticalFlag to false.
2.6 void Update()
- Continuous check for horizontal input.
- Continuous check for vertical input.
- If horizontalFlag becomes true, call HorizontalRotation().
- If verticalFlag becomes true, call VerticalRotation().
I hope you find this blog is very helpful while working with Smooth Rotation object in Unity. Let me know in comment if you have any question regarding Smooth Rotation Object.
Got an Idea of Game Development? What are you still waiting for? Contact us now and see the Idea live soon. Our company has been named as one of the best Game Development Company in India.
- Smooth Rotation of Object in UNITY
- Get translation and rotation matrix of an object
- Smooth scrolling in Android
- No. 47 - Search in a Rotation of an Array
- Unity GUI Rotation
- Unity面板中的Rotation
- Perfect smooth scrolling in UITableViews
- Perfect smooth scrolling in UITableViews
- 在Unity中通过The touch screen implements the rotation and scaling of the model
- Smooth
- Fundamentals of Object-Oriented Design in UML
- Process of creating an object in Java
- Thinking In Java:Characteristics of Object-Oriented
- (for..in)、Object.keys()和Object.getOwnPropertyNames(),for...of
- Smooth Image Flicking in Qt Quick
- Smooth Pixel Scrolling in WPF ListView / ListBox
- Tricks of Serialization in Unity/C#
- Hackerrank - Game Of Rotation 题解
- ACM: 动态规划 poj1695
- ACM: 暴力题 poj 1338
- 想想
- ACM: 变形的最大公共自序列 动态规…
- ACM: 动态规划 poj 2192
- Smooth Rotation of Object in UNITY
- ACM: 动态规划 华东师范OJ 1244 …
- ACM: 动态规划 黑书上的题目
- ACM: 搜索题 poj 1691 一觉醒来居…
- ACM: 动态规划 poj 1014
- ACM: 二分图最佳匹配 图论题 poj 3…
- ACM: 有向欧拉图 图论题 poj 1386 …
- ACM: 二分图最大匹配 图论题 poj 2…
- Largest prime 尤拉托斯展那筛选法