• 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 /
avatar image
0
Question by Hotsaucehater · Sep 29, 2015 at 09:44 PM · 2dcolorendless runner

Advanced(ish) sprite color help

Hi guys,

So I'm not really sure how to describe what I want to do here, but I'll give it my best shot.

So I an going to have a gray 2D sprite (aka the ground) constantly moving to the left on my screen (think an endless runner), and I will have a player sprite on it. As the ground sprite passes under the player sprite, I would like to make the color of the ground change to red, but ONLY past the player. I have drawn a concept of what I'm going for below:

Concept

The red square represents the player, and the red/gray line is the ground before and after the player passes over it. The arrow is the direction the player is traveling.

Has anybody ever done something like this? Should I use some sort of empty GameObject? Is this even possible? It would be of great help if someone could point me in the right direction.

Thanks :)

color-change-concept.png (500 B)
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

3 Replies

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

Answer by Jessespike · Sep 30, 2015 at 03:05 AM

There are several ways you can probably accomplish this. If you're using simple shapes for ground like in the example provided. You could make the ground into 2 shapes (one gray, one red). The red layer's width would be scaled to 0, as the player runs by, the red layer's scale will increase. Similar to how progress bars or sliders have a fill amount value of 0 to 1. You would need to calculate the shapes position and width in accordance with the player's position to know when to increase the fill amount on the current shape that's directly under the player.

Edit: I was able to get results with this script. Have to set the Pivot on the Sprites to Left. "The ground" is a GameObject that contains this script, and it has 2 children Sprite GameObjects, named Gray and Red respectively.

 using UnityEngine;
 
 public class FillableBlock : MonoBehaviour {
 
     public Transform _Player;
     public Renderer _Gray, _Red;
     
     // Update is called once per frame
     void Update () {
         float distanceFromBlock = _Player.position.x - _Gray.transform.position.x;
         float fillAmount = Mathf.Clamp(distanceFromBlock, 0f, _Gray.bounds.size.x) / _Gray.bounds.size.x * _Gray.transform.localScale.x;
         _Red.transform.localScale = new Vector3(fillAmount, _Red.transform.localScale.y, _Red.transform.localScale.z);
     }
 }
 
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 Hotsaucehater · Oct 02, 2015 at 07:42 PM 0
Share

This is a good solution. Although the ground will be more complicated than the example provided (sorry, I should have been more specific), I can split it into small straight pieces and use this. Thanks!

avatar image
1

Answer by daniel-eherbert · Sep 30, 2015 at 04:42 AM

@Jessespike is on the money there. That would by far be the simplest solution.

It depends a bit on how complex the ground will be too. If you're going to have different/complex shapes it becomes a bit more tricky.

There may be different approaches, but I can think of 2 which use shaders:

  1. Have two copies of your ground/terrain, one red and one grey. Then a custom shader could set the opacity of each ground (left for grey, right for right) based on the position of the player.
    • You would have a colliders on only one of them I suspect

  2. Have a single terrain item, but a custom shader that renders a different color based on the position of the player (to left of player = red, to right of player = grey)

Comment
Add comment · 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
0

Answer by RChrispy · Sep 30, 2015 at 08:05 AM

I would just use a world canvas for the ground with a mask on it and then blend two images. [almost zero coding]

You can plant an collider behind them, and dont need to code anything except the scaling of the width of the images RectTransform ( dont forget to anker it to the left ).

Cheers :)

Comment
Add comment · 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

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

34 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

Related Questions

Changing two different objects renderer colour 1 Answer

Incorrect Sprite Color on Android build 0 Answers

How to render a colored 2D rectangle. 6 Answers

Is there a way to drag a public color into another object's color field? 1 Answer

Material doesn't have a color property '_Color' 4 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