Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
1
Question by Randozart · Jun 03, 2016 at 12:46 PM · c#guicolorimageshake

Shake UI Image/Gradually Change Color

Hello!

I've recently picked up learning Unity, and by that extent C#. As such, I do not yet have a very extensive understanding of C# coding, and am, as such, making a couple of simple projects to get the hang of it. But I've run into a bit of a problem I can't find the proper function/solution for:

alt text

Upon the opponent being hit, I want to invoke a function (or two functions) to do two things:

  1. The image shakes from left to right

  2. The image briefly turns red (or a light shade of red)

This, of course also goes for the player character upon being hit. I've scoured the internet trying to find the right functions or code to use, but every time I type in similiar questions it usually only refers to camera shake. What code do I need to use to accomplish this?

simplerpgbattle.png (224.1 kB)
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
2
Best Answer

Answer by hfpngaming · Jun 04, 2016 at 04:32 AM

This should work and fit your needs, but you might want to tweak it to your needs. Also my script is called by pressing space, you can change that to being called when damage is dealt.

 public class EnemyDamage : MonoBehaviour {
 
     public GameObject enemy;
     private SpriteRenderer enemyRenderer;
 
     public float damageTime = 0.1f; // duration of shake and red color
 
     public float shakeRange = 20f; // shake range change be changed from inspector,
                                    //keep it mind that max it can go is half in either direction
 
     // Use this for initialization
     void Start () {
         //by saving the renderer you will not need to use GetComponent<> again and again
         enemyRenderer = enemy.GetComponent<SpriteRenderer>();
     }
     
     // Update is called once per frame
     void Update () {
         if (Input.GetKeyDown(KeyCode.Space))
         {
             StartCoroutine(Damage());
             StartCoroutine(EnemyShake());
         }
     
     }
 
     private IEnumerator Damage()
     {
         Color originalColor = enemyRenderer.color;
         WaitForSeconds wait = new WaitForSeconds(damageTime);
         enemyRenderer.color = new Color32(255, 0, 0, 255); //adjust color to your needs
         yield return wait;
         enemyRenderer.color = originalColor;
     }
 
     private IEnumerator EnemyShake()
     {
 
         float elapsed = 0.0f;
         Quaternion originalRotation = enemy.transform.rotation;
 
         while (elapsed < damageTime)
         {
 
             elapsed += Time.deltaTime;
             float z = Random.value * shakeRange - (shakeRange /2);
             enemy.transform.eulerAngles = new Vector3(originalRotation.x, originalRotation.y, originalRotation.z + z); 
             yield return null;
         }
 
         enemy.transform.rotation = originalRotation;
     }
 }
Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Randozart · Jun 04, 2016 at 09:27 AM 0
Share

Thanks alot! :D This really has saved me a from a headache or two.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

179 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Change UI image in a conversation 1 Answer

Image Colour change not working with anythign other than base preset colours 2 Answers

Changing image color from other object's script (glitch)? 0 Answers

Change colors for beginners 2 Answers

Save Image state between scenes 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges