Understanding Movement and Rotation in C#

Rotate Around Arbitrary Axes

Your browser needs to be JavaScript capable to view this video

Try reloading this page, or reviewing your browser settings

In this video segment, learn how a game object can rotate around another object using Tranform.RotateAround.

Keywords

  • Pivot point
  • Rotate
  • Game object
  • Rotation speed
  • Transform.RotateAround

About this video

Author(s)
Alan Thorn
First online
12 January 2019
DOI
https://doi.org/10.1007/978-1-4842-4442-5_10
Online ISBN
978-1-4842-4442-5
Publisher
Apress
Copyright information
© Alan Thorn 2019

Video Transcript

[Audio Begins] [0:00:00]

Alan Thorn: In this movie, we’re going to see how one object can rotate around another. That is how one object can use another one as the pivot or center of its rotation. In particular, we going to look at how object X, let’s say, for example, a bee character can fly around a tree or rotate around another object. To demonstrate rotation around other object, I’m going to be using the sample scene that we’ve carried through throughout the preceding videos of this course. The important thing to remember when we’re rotating around other objects is that fundamentally it is not so much rotation that is involved, but really movement. Rather, we’re moving an object on an arch or a circular path around another object. That’s what it means to rotate one object around another.

To do this, I’m going to be focusing on the tree over here. So this particular tree object. I’m going to create an imaginary other object that’s going to rotate around it. Perhaps it could be a bird, or a bee, or a butterfly, or some other kind of object that we want to have fly around this tree. To demonstrate this, I’m going to start by creating the object that’s going to fly around the tree, and to do that I’m going to choose Game Object, 3D object, and I’m going to use a sphere. I’m going to position the sphere slightly away from the tree object and slightly further up. And scan that down just a little bit and move it over to here. Effectively, this object is going to be flying in a circular path around the top of this tree. To define the top of this tree, I’m also going to use a game object. By choosing Game Object, Create Empty, and I’m going to name this rotation pivot, like so. And I will locate this up here to the center of the tree to about there.

To ensure that I can see this in the view port, I’m going to assign this an orange tag just so that I can see the location of the pivot point. I’m going to select the sphere object and simply name this 2B, to give an indication as to the kind of thing this could be. I’m going to move to the scripts folder. Right-click and choose Create, and choose C# script. And I’m simply going to call this rotate around, and press enter in the keyboard to accept that. I’ll drag and drop the rotate around script onto the bee object. So I’m going to select the bee object here, and have the rotate around script. I can open this up inside visual studio to start piecing together our script file. Now the first thing that we’ll need for this is, in fact, the thing that we want to rotate around, so I’m going to select that here. In particular, I’m going to have a reference to the transform component that’s going to be our pivot object. So initially, it’s going to equal null. We’re going to specify the value of this from the object inspector.

In addition to this, I need a reference to the transform component attached to the bee object itself, which we’ve seen many times before. I’m going to use the awake function to get access by using get component. So I’m going to choose Get Component here, and access the transform component. The next thing we want, is we want a rotation speed. Once again, remember this is not really the speed of rotation, but the speed of movement around which the bee will fly around the tree. To do this I’m going to define a public floating point variable that I’m going to call move speed, and it’s going to move at a speed of 1 meter per second. So I’m going to save that. Inside the update function I’m going to update the motion of the bee, but essentially, I’m going to access the transform component and run the function that says rotate around. Now, the rotate around function requires primarily two values here. One is the axis, the axis around which it will rotate. And then the angle here. We have some other variables or versions of this function here that I want to draw your attention to.

I’m going to be using this version. That is to specify the point of rotation, that is the point away from us around which we will rotate. And the second function, or the second input here, is the axis itself. This is going to define the orientation around that point around which we will rotate. And then we also have the angle. So in this case, the first parameter is simply going to be – this is our pivot destination here, pivot.position. The second argument is going to be pivot.up. And the third argument is going to be our movement speed multiplied by Time.deltaTime. I’m going to press Ctrl-S to save that data and turn back to the scene here.

I’m going to make sure that for our bee script here inside the inspector, that I have specified the pivot around which we’re going to rotate. Right now, the pivot specified is none. I can fix that easily by dragging and dropping rotation pivot into that field and pressing Play on the toolbar. Now, that I do that you can see that now this is beginning to rotate around. And I’m going to increase the speed to maybe something like 60. And now all of a sudden you can see this object is rotating around the specified destination. So that is how we can make one object, in this case the bee, rotate around a completely separate pivot point.

In this movie, we saw a specialized version of rotation. In particular, we saw how we could make one object rotate around another. To achieve this, we used the transform.rotatearound function.

[Audio Ends] [0:06:13]