Unity learns the color change when the note is damaged

Source: Internet
Author: User

Recently in the video to do, very simple demo

That's it, the people who have seen it, they know it's official. Just four levels, I can only find some domestic re-system to see

Now want to do the effect is to be hurt when the red, let slowly smooth transition to the white

In the Update method, the following

    

void Update ()    {
Mouse Left click Simulation is harmed if(Input.getmousebuttondown (0)) { takedamage (30f); } Color becomes white =color.lerp (BodyRenderer.material.color, color.white, smooth * time.deltatime); }

Of course, the color will turn red in the takedamage method. There's no problem.

Now want to add a judgment to the color back to the point of optimization, the idea is to judge the current color is red to become white, very simple plus if

void Update ()    {        if(Input.getmousebuttondown (0))        {            takedamage (30f);        }         if (BodyRenderer.material.color.Equals (color.red))        {            = Color.lerp (BodyRenderer.material.color, color.white, smooth * time.deltatime);        }    }

Run, and then just:

It doesn't turn white,

Open Breakpoint Debugging

The specific process does not repeat, anyway later color into the figure shown, the system red color RGB is

At this point, color.lerp is gradually changing color, so although it looks red, but it is different from red, so can not trigger discoloration

Judging the condition as not white

void Update ()    {        // Mouse left click to simulate damage        if (Input.getmousebuttondown (0))        {            takedamage (30f);        }
The judging condition is set to not white if(! BodyRenderer.material.color.Equals (Color.White)) { // color becomes white BodyRenderer.material.color = Color.lerp (BodyRenderer.material.color, color.white, smooth * time.deltatime); } }

I do. NET, chance coincidence under the contact unity, feel very interesting, personally think this and do program development have certain similarities and differences, now still familiar with the API stage, Master good tools to make good things ah

To summarize:

1. Breakpoint debugging must be mastered, but unity debugging is different, he is every frame call once, want to know specifically can see I am studying the unity life cycle

2. Beginners are best to find some ready-made examples, recommend the official, good quality, complete, and free, do not let the model or anything disturbing the mentality of learning.

3. Some time ago to see an article in the garden, said to think, learn things to think, may not understand, but if you see the script copy came running under the control,

Maybe you can do nothing when you do it yourself.

Unity learns the color change when the note is damaged

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.