HOW TO MAKE AN OBJECT MOVE AND ROTATE IN UNITY

In the previous lesson, we have learned the basics of unity. From getting familiar with unity tools, importing assets, and how to make the assets have its own components and scripts. If you haven't tried that out yet, you may visit it here: How to add and make an object move in Unity

Today's activity, we'll be needing three assets: ball, stick, and boxes. After importing the assets, position it as shown. 


Following are the components for each asset:
            BALL - RigidBody 2D (Gravity Scale 0), Circle Collider
            TWO BOXES - Box Collider 2D
            STICK - Box Collider 2D

Our next step is to create a distinct scene for the two boxes. We have to create another scene for each for the next position of the boxes. 



In this example, we created two scenes: movingBox1 and movingBox2. Under movingBox1, we have the asset box1 and the next position scene named posNxt1.
Same goes for the other scene.

For box1, we will assign its next position by clicking the posNxt1 scene first, and selecting the Move Tool. Notice that there will be a right angled figure that will appear in the Scene Panel. That indicates that it will be the next position of the selected asset, box1.



Do the same for box2. Make sure to select the scene first before moving the asset.

After assigning the next position for each box, we now have to create a script that contains this code for each box: MovingBox1 and MovingBox2 scripts.

Scripts for each box






Assign these scripts to the two boxes. Click the movingBox1 scene and in the Inspector Panel, assign box1 as Child Trans[form] and posNxt1 to Trans[form] B. Do the same for movingBox2 scene with its corresponding asset and next position scene.

If we press play, the two boxes should start moving back and forth from its initial and next positions. If not, double check for the assignments of the scripts and scenes.

Now we only have to add the script for the stick. Create a script containing this code and add it as a component of the stick.


Click play. The boxes are now moving and the stick it rotating 360.

We now only have to add the script for the ball which we have tackled in the previous lesson

And we're done. The game should look like this. 






Comments