Gamespace: Collectable Item Coded Animation

After successfully picking up one ball, I duplicated this object and placed them all over the landscape.


Items spread across landscape

To make the object more interesting I wanted to add movement and rotation to the object as all items were completely still.


Item

I attached two scripts to the Collectable Item inspector 'Idle Move' and 'Constant Rotation' where the amplitude was set to 0.15, the frequency was set to 2 and the speed of rotation was set to 90.


Collectable Item Inspector

The 'Idle move' script has two float variables 'amplitude' (how far up the item goes when moving) and the 'frequency' declared as public meaning that they can be edited in the inspector.
The Vector3 variable 'initialPosition' is set as private meaning that it cannot be edited outside of the script.

The initial position of the item is assigned in the 'Start' function. The value assigned to the 'initialPosition' variable is set by finding the local position of the item using transform.localPosition.

The y-value float variable is declared and is equal to a Sin wave multiplied by the amplitude which is used to give the item the up and down animation.

The item is given this new position using transform.localPosition. This is set to being equal to the item's initial position plus a new 3D vector with the X-axis being equal to zero, the Y-axis being equal to the variable 'y-value' and the Z-axis being equal to zero allowing the item to constantly and repeatedly move vertically until the player has collected the item.

Idle Move Script

The 'ConstantRotation' script has the float variable 'speed' set as public meaning that the value of this variable can be edited in the inspector.

To rotate the item constantly, the object is rotated using a 3D vector which is multiplied by the speed variable and by real time.


Constant Rotation Script

Unfortunately, because I wanted my item's material to be a dark blue, the constant rotation on the item isn't as noticeable as having a pattern or design.

Comments

Popular posts from this blog

Unity Project 4: Ball platformer game

[Research] Traditional Poster Design (4CTA1214)