Search icon CANCEL
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Unity 5.x 2D Game Development Blueprints

You're reading from   Unity 5.x 2D Game Development Blueprints Explore the features of Unity 5 for 2D game development by building three amazing game projects

Arrow left icon
Product type Paperback
Published in Sep 2016
Publisher Packt
ISBN-13 9781784393106
Length 252 pages
Edition 1st Edition
Languages
Tools
Arrow right icon
Authors (2):
Arrow left icon
Francesco Sapio Francesco Sapio
Author Profile Icon Francesco Sapio
Francesco Sapio
Abdelrahman Elsayegh Abdelrahman Elsayegh
Author Profile Icon Abdelrahman Elsayegh
Abdelrahman Elsayegh
Arrow right icon
View More author details
Toc

Animating the hero


We are now going to create a script that moves the weapon and the shield items upward then in a downward fashion to simulate a simple breathing look. Let's create another C# script and name it Breather inside the Scripts folder. Next, attach the script to both the weapon and shield object under the player. Double-click on the script to open it.

First we need to add a couple of variables to store the local position to make our hero breath:

    public Vector2 position1; 
    public Vector2 position2; 

Then, a third variable stores the time between the two positions defined before:

    public float waitTime; 

In the Start() function, we start the coroutine we are going to implement:

    void Start() { 
        StartCoroutine(Mover()); 
    } 

Finally, in our coroutine, we start at a random moment and then there is a main loop that changes between the two positions:

    IEnumerator Mover() { 
        yield return new WaitForSeconds(Random.Range...
lock icon The rest of the chapter is locked
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at €18.99/month. Cancel anytime