Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 /
avatar image
0
Question by MartyBD · Apr 12, 2018 at 10:50 PM · axiscolor changecolour

Assigning R,G,B values to X,Y,Z to change colour in accordance with changing position

I'm looking for a way to assign red to X axis, green to Y axis and blue to Z axis. This is so I can move an object through the unity environment (say in a 5x5x5 space) and it will change colour accordingly.

So if it was in a random position of (1,3,1) then its colour would be (80,150,80) causing the object to turn a green shade. - **these values are just random to be used as an example, not how i expect it to work

This is assuming colours go from 0-255 values.

So on the X axis, from positions 1 to 5 the colour would change from red 0 to 255 (or however unity decides on colour)

I have very little experience with Unity.

Thanks

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
1

Answer by Chronos-L · Apr 14, 2018 at 08:23 AM

well... you already have the core concept, just put them together.

 Transform _t;
 Material _m;
 
 void Awake()
 {
     _t = GetComponent<Transform>();
    _m = GetComponent<MeshRenderer>().material;
 }

 void Update()
 {
    Vector3 pos = _t.position;    // Getting position of your gameobject
    //pos /= 5 * 255;               ///(since you are mapping 0...5 to 0...255) Incorrect
    pos /= 5;
    _m.color = new Color(pos.x, pos.y, pos.z);
 }
 
Comment
Add comment · Show 4 · 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 Bunny83 · Apr 14, 2018 at 09:34 AM 0
Share

"Color" is in the range 0 - 1, not 0 - 255. So just do

 Vector3 pos = _t.position / 5;
 _m.color = new Color(pos.x, pos.y, pos.z);

This of course assumes a worldspace range of 0 - 5 in each axis. The easiest, inspector adjustable solution would be to use an empty gameobject in the scene. It's origin would define the starting corner (equal to black) and (1,1,1) in local space would be the opposite corner (equal to white). You can scale the object to whatever size you like (5,5,5) in your example.

 Transform space; // our empty gameobject
 
 Vector3 pos = space.InverseTransformPoint(_t.position);
 _m.color = new Color(pos.x, pos.y, pos.z);

This way you can move, rotate and scale your "space" however you like.

avatar image Bunny83 · Apr 14, 2018 at 09:37 AM 0
Share

ps: this line:

 pos /= 5 * 255; 

doesn't even map 0-5 to 0-255 but maps the space 0-1275 to 0-1. x/=y is a short hand for: x=x/(y). So your line would be

 pos = pos / (5 * 255);
avatar image Chronos-L Bunny83 · Apr 15, 2018 at 12:52 AM 1
Share

Apologies, that was very sloppy of me. Thanks for the correction.

avatar image MarioSantoso · Apr 14, 2018 at 01:49 PM 0
Share

Follow @Bunny83 comments, color range is 0 - 1.

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

79 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

Related Questions

Changing the colour of my shaders through GUI Colour palette? 0 Answers

How to slowly change the colour of a directional light object 3 Answers

User should be able to pick a Gradient color and apply them linear or circular from the touch point taken as the origin. 1 Answer

Find a Position on the Axis of a GameObject 1 Answer

Mouse axis wont change an integar. 1 Answer


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