• 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
Question by NPstudio · Jun 30, 2015 at 04:56 PM · scripting problemshadercollidermaterialdynamic

Change Material depending on Collider position?

Hello Everybody! Good morning or afternoon or evening, depending where you are!

Here's the deal, I am working on a game which requires me to create little "Stasis Zones" as in the character spawns a Sphere and everything inside it freezes, I got the textures for it but for the life of me I cannot find a way on the interweb that explains how to do the following:

Imagine a Player, he presses E and in a 4 feet radius it spawns a sphere rotating on itself with a cool texture, everything inside this sphere changes texture, which is easy if you're already in the sphere;

But what if a Box is halfway through the sphere collider? I would like that only the part inside the zone changes material, while the other stays the way it is;

Here is a visual example of what I'm talking about: alt text

example.png (24.2 kB)
Comment

People who like this

0 Show 2
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 IphStich · Jun 30, 2015 at 05:19 PM 1
Share

I would write a shader to do this. Where for each fragment, it would either render normally and display the default texture, or display the secondary texture, depending on its distance from the centre of the sphere (and therefore whether it's inside it or not).

I can't promise this is the best solution, and can't exactly provide sample code right now.

avatar image Japa · Jun 30, 2015 at 05:37 PM 1
Share

Yeah, that would definately be the way to go. The shader manual has an example on how to do things by world space coordinates. You just need to pass the parameters of the sphere to the shader.

2 Replies

· Add your reply
  • Sort: 
avatar image
Best Answer

Answer by NPstudio · Jun 30, 2015 at 07:33 PM

For everyone out there I used Stencil Shaders, they do the trick awesomely~

Comment

People who like this

0 Show 0 · 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

Answer by karp505 · Jun 30, 2015 at 10:59 PM

Hi, this works for me:

 Shader "Custom/shader" {
     Properties{
         _Color ("Color", Color) = (1, 1, 1, 0.5)
         _ForceField ("Force Field Position", Vector) = (0, 0, 0)
     }
     SubShader {
         Tags{ "Queue" = "Geometry"}
 
         Pass{
 
             CGPROGRAM
 
             #pragma vertex vert
             #pragma fragment frag
             #include "UnityCG.cginc"
 
             uniform float4 _Color;
             uniform float4 _ForceField;
 
             struct vertexInput{
                 float4 vertex : POSITION;
             };
 
             struct vertexOutput{
                 float4 pos : SV_POSITION;
                 float4 worldPos : TEXCOORD0;
             };
 
             vertexOutput vert (vertexInput input){
                 vertexOutput output;
 
                 float4x4 modelMatrix = _Object2World;
 
                 output.pos = mul(UNITY_MATRIX_MVP, input.vertex);
                 output.worldPos = mul(modelMatrix, input.vertex);
 
                 return output;
             }
 
             float4 frag(vertexOutput input) : COLOR{
                 float4 color = _Color;
                 if (distance (input.worldPos, _ForceField) > 2.7)
                     color = float4(0.0,0.0,0.0,1.0);
                 return float4 (color);
             }
             ENDCG
         }
     } 
 }
 

Just put that a material with that shader on your boxes and set the Force Field Position property in the editor to wherever your sphere is (or do it through script). Also, change the distance in this line:

  if (distance (input.worldPos, _ForceField) > 2.7)

in accordance with how large the sphere is. 2.7 is good for a standard unity sphere with dimensions 5x5x5. Hope that helps! Let me know if you have any questions.

Comment
NPstudio
Toscan0

People who like this

2 Show 0 · 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

24 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

Related Questions

How come edititng "_Glossiness" through script doesn't work? 1 Answer

Material doesn't have a color property '_Color' 4 Answers

HDRP Lit Shader: How to disable Transparency "Blend preserve" through script? 0 Answers

Changing Emission Scale UI with Script 0 Answers

How do I get rid of the black line in Unity Sky 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