How To Get Data From Another Script In Unity
Unity Unproblematic 2D Rotation Script.
While working in Unity I found it difficult to figure out how to become an object to rotate. If you're reading this, yous probably did likewise. Right now I'thousand just talking near second game object rotation. The concept works in 3D as well, only turn on a different axis, but I won't go into that here.
If you lot don't feel like reading the article and but want the Full Script… skip to the lesser.
Add this to the holding section of your class:
public float degreesPerSec = 360f ; |
This will be the variable that tells the script how fast to rotate around the axis. It'south in degrees per second. Feel gratis to play with the number equally see how information technology affects the rotation speed.
Add this to the Update method of your course:
bladder rotAmount = degreesPerSec * Time . deltaTime ; bladder curRot = transform . localRotation . eulerAngles . z ; transform . localRotation = Quaternion . Euler ( new Vector3 ( 0 , 0 , curRot + rotAmount ) ) ; |
The first line only takes the degreesPerSec variable and multiplies by the delta time, to give us the fraction of degressPerSec to use this frame.
The second line gets the current rotation of the z-axis of the object. We utilize the z-axis considering the x and y centrality are being used to motion left and right and upward and down. Basically to brand a object seen in 2 dimensions, have those dimensions announced to rotate, they must exist turned in a 3rd dimension, but that'south a conversation for some other fourth dimension, just trust me and apply the z-axis.
The third line resets the localRotation to a new Vector3, with 0 ten rotation, 0 y rotation, and a z-axis rotation equal to the last z-axis rotation stored in curRot, added to rotAmount, the fraction of the degreesPerSec to rotate per frame.
Why use transform.localRotation instead of transform.rotation?
Well the thought here is that when you utilize transform.rotation, it returns the objects rotation in global space, and turn it, ow affair what the rotation of information technology'due south parent is. Using localRotation turns the object with respect to it's parent rotation. Feel costless to interchange them, and view the effects, and run across which one fits what you are trying to achieve.
What is Quaternion.eulerAngles?
Objects in Unity are rotated using a concept called a Quaternion. Information technology allows objects to be rotated in any way in 3D infinite, and are very hard to visualize. They utilise x,y,z,and due west, and some imaginary number i,j, and k to work. Luckily they can be created using Euler Angles which is what nosotros all know and dearest, and the method I utilize in the example. If you want to know more about Quaternions, don't ask me, watch this helpful youtube video.
This guy knows what he's talking near.
Total Script :
i 2 3 4 5 6 7 viii 9 10 11 12 13 xiv 15 16 17 | using UnityEngine ; using System . Collections ; public course Rotation : MonoBehaviour { public float degreesPerSec = 360f ; void Start ( ) { } void Update ( ) { float rotAmount = degreesPerSec * Time . deltaTime ; bladder curRot = transform . localRotation . eulerAngles . z ; transform . localRotation = Quaternion . Euler ( new Vector3 ( 0 , 0 , curRot + rotAmount ) ) ; } } |
The following 2 tabs change content below.
- Bio
- Latest Posts
Game Designer, Programmer, Lover of all things retro. I've been a programmer for well-nigh 8 years. Got into game development function time vi years ago and haven't been able to put it down.
How To Get Data From Another Script In Unity,
Source: https://weeklycoder.com/2015/07/20/unity-simple-2d-rotation-script/
Posted by: wagnergear1974.blogspot.com
0 Response to "How To Get Data From Another Script In Unity"
Post a Comment