Journal Scripting Walkthrough 2


Part 3


Date: 25.3.2024

Activity:

⁃  Creating the Power-Up Object (Key)

      -> Writing the PickingUp Script:

      -> Implemented logic inside OnTriggerEnter2D() to detect collisions with the player character.

⁃  Implementing the PlayerEffects Script:

      -> Implemented the TemporaryUpEffect() coroutine inside GetSuperpowers(), which temporarily increases the character's size by 1.5 times and then returns it to its normal size after the specified duration.

⁃  Character Interaction with the Key:

      -> When the player character interacts with the key, it triggers the itemEffect.Invoke() method, activating the growth effect.

      -> The character's size changes for the specified duration, as implemented in the TemporaryUpEffect() coroutine of the PlayerEffects script.

Notes:

In Part 3 of the tutorial, it was explained how to add power-up objects. I created a key that, upon interaction with the playable character, increases their size by 1.5. To begin, I created an empty object named PowerUp. Then, in the Inspector, I added Sprite Renderer and Capsule Collider 2D components to it. In the Sprite Renderer component, I added a sprite that looks like a key. Next, in the Capsule Collider 2D component, I configured IsTrigger to be enabled, so that I could track if the character touched the object.

I wrote a script called PickingUp for collision detection, which I added as a component to the key object. In this script, I added a method OnTriggerEnter2D(). In this method, it was necessary to check if the key collided with the character. If it did, the method itemEffect.Invoke() is called, which activates the item's effect using UnityEvent. After obtaining a reference to the collider of the item to which this script is attached, the collider is temporarily disabled to prevent the trigger from using again. Additionally, once disabled, the key's effect cannot be used again. After 10 seconds, the method EnableItem() is called using Invoke, which re-enables the Collider component, making the item active again for collisions and allowing the effect to be used again.

The second script is PlayerEffects, which is added as a component in the inspector of the playable character. This script implements the method GetSuperpowers(), which takes a parameter called duration as the duration of the growth effect and uses a coroutine called TemporaryUpEffect().

GetSuperpowers() method calls the coroutine TemporaryUpEffect() passing the duration of the effect. TemporaryUpEffect() coroutine sets the character's size to 1.5 times larger than normal to apply the "superpowers" effect. Then, it waits for the specified number of seconds (duration) using yield return new WaitForSeconds(duration), after which it returns the object's scale to its normal value.

After all these actions, the character could interact with the key and change in size for a certain period of time.

Invested hours:

 ⁃ Creating key & scripts: 1h 30 min

⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯

Part 4


Date: 30.3.2024

Activity:

⁃ Animation of an object:

      -> Rotation around its axis.

      ->  Levitation of an object along a given trajectory using curves.

⁃ Creating different types of animations and their different ways of triggering:

      -> Through a script.

      -> Through the Animator window.

⁃ Accessibility and unavailability in the use of the object.

⁃ Adding a sound component and using it.

Notes:

In Part 4 of the tutorial, I learned how to animate the power-up object designating the key. All the code continued in the familiar PowerUp script. As a basis in the tutorial, it was said to take the key from Part 3 that I had already prepared. For animation, I learned how to use the Animation Window to create a real animation from a set of pictures, how to use curves to create a key levitation trajectory, how to use Preview mode and Add keyframe button. Also how to make the object accessible and unavailable for use (functions EnableItem() and DisableItem()). I have created two types of animation, PowerUpSpin and PowerUpNonSpin, where the first animation is an animation of a key without contact with a character, and the second animation is played after contact with him.

Also, the most interesting and difficult part was to use the Animator Window. It so happened that when I started working with this window, my correct key animation crashed. The animation consisted of the fact that while the character is not in contact with the key, the key can levitate along a given trajectory, rotate cyclically around its axis and leave its usual color (PowerUpSpin animation). As soon as the character and the power-up object are crossed, the animation is not performed and the object becomes translucent (PowerUpNonSpin animation). The key will resume its animation when it has its abilities available after a certain period of time (PowerUpSpin animation). But what happened to me was that after starting the game, the key performed one revolution around its axis, stopped and became translucent. Then, after contact with him and the decrease of a certain period of time, there was already the usual animation of the key described above (PowerUpSpin animation). I couldn't figure out what the problem was for a long time, so I had to call the EnableItem() function in the Start() function. After that, the animation became as it was intended. Finally, I added the PlaySound() function, which allowed me to play the sound of the key levitation on a certain animation image.

Subsequently, in the steps described in the tutorial, struggling with one problem, I got a working animation of the power-up object.



Invested hours:

 ⁃ Animation & script: 3h 24 min

Outcome:

ScriptingWalkthrought2 (uploaded to the itcho.io page as a .zip file)

Files

ScriptingWalkthrought2.zip 3.6 kB
Mar 31, 2024

Get Project T

Leave a comment

Log in with itch.io to leave a comment.