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
0
Question by BrodyDaChappy · Jul 04, 2016 at 08:11 PM · 2dcolorplatformerplatformcolors

Gradient on Platform

Hi All,

I am trying to create a color gradient on a platform, see picture 1 for example, that is what i currently have but i have just used multiple mesh's and gameObject.renderer.material.color = Color.green; using OnTriggerEnter2D, also i have copy's of those using the same method but for score.!alt text

Me and a friend are in the middle of changing this but are getting position problems (see Debug.Log IMG)alt text

We have the gradient all working! but the error is the positioning of the gradient, the debug is showing all different positions meaning that the gradient only changes part of the platform, (which will now be 1 single mesh).

Below is the Shader/s used and the new color change script.

using UnityEngine; using System.Collections;

public class ColorChange : MonoBehaviour {

 public bool changed = false;
 private Mesh mesh;
 private Vector3 childTransform;
 private Vector3[] worldPositions;
 private Color[] colors;


 void Start()
 {
     this.mesh = GetComponentInChildren<MeshFilter>().mesh;
     childTransform = GetComponentInChildren<Transform>().position;
     colors = new Color[mesh.vertexCount];
     for(int i = 0; i < mesh.vertexCount; i++)
     {
         colors[i] = Color.white;

         worldPositions = new Vector3[mesh.vertexCount]; 
         worldPositions[i] = childTransform + mesh.vertices[i]; 
         Debug.LogWarning (string.Format ("Object Position: {0} || Vertex Position: {1} || New Position: {2}", childTransform, mesh.vertices[i], worldPositions[i]));
     }
 } 

 void OnCollisionStay2D(Collision2D other)
 {
     if(other.gameObject.tag == "Player")
     {
         int closestVertex = 0;

         for(int i = 1; i < worldPositions.Length; i++)
         {
             if (Vector3.Distance(worldPositions[i], other.transform.position) < Vector3.Distance(worldPositions[closestVertex], other.transform.position))
             {
                 closestVertex = i;
                 Debug.Log (string.Format("Closest vertice is: {0}",i));
             }
         
         }
         colors[closestVertex] = Color.green;
         Debug.Log (string.Format ("Vertex Color changed: {0} on {1}", colors[closestVertex], mesh.vertices[closestVertex]));
         mesh.colors = colors;

         if(changed)
             return;
         
         for(int i = 0; i < mesh.vertexCount; i++)
         {
             if(colors[i] != Color.green)
             {
                 changed = false;
                 break;
             }
             changed = true;
         }
     }
 }

}

Shader "Custom/VertexColor" { Properties { _Color ("Color", Color) = (1.00, 1.00, 1.00, 1.00) } SubShader { Tags { "RenderType"="Opaque" } LOD 200

  CGPROGRAM
  #pragma surface surf Lambert
  fixed4 _Color;
  struct Input {
      float2 uv_MainTex;
      float4 color : COLOR;
  };
  void surf (Input IN, inout SurfaceOutput o) {
      
      o.Albedo = IN.color.rgb * _Color.rgb;
      o.Alpha = IN.color.a * _Color.a;
  }
  ENDCG

} FallBack "Diffuse" }

Shader 2 Shader "Custom/VertexColor2" { Properties {

} SubShader { Tags { "RenderType"="Opaque" } Pass {

      CGPROGRAM
      #pragma vertex wfiVertCol
      #pragma fragment passThrough
      #include "UnityCG.cginc"
      struct VertOut 
      {
          float4 position : POSITION;
          float4 color : COLOR;
      };
  
      struct VertIn
      {
          float4 vertex : POSITION;
          float4 color : COLOR;
      };
      VertOut wfiVertCol(VertIn input, float3 normal : NORMAL)
      {
          VertOut output;
          output.position = mul(UNITY_MATRIX_MVP, input.vertex);
          output.color = input.color;
          return output;
      }
  
      struct FragOut
      {
          float4 color : COLOR;
      };
  
      FragOut passThrough(float4 color : COLOR)
      {
          FragOut output;
          output.color = color;
          return output;
      }
      ENDCG
  } 

}

picture-1.jpg (5.8 kB)
debuglogposition.jpg (91.1 kB)
Comment
Add comment · Show 6
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 AddyDaDaddy · Jul 11, 2016 at 02:41 PM 0
Share

bump......

avatar image AddyDaDaddy AddyDaDaddy · Jul 11, 2016 at 02:45 PM -1
Share

@Light FYI

avatar image Jessespike · Jul 11, 2016 at 06:58 PM 0
Share

There's not enough information to answer. You say the gradient is all working, but then say there's a problem. Perhaps paste the script here or show some screenshots of what is happening and what you actually want to happen.

avatar image BrodyDaChappy Jessespike · Jul 12, 2016 at 08:17 PM 0
Share

Hi @Jessespike, i will update the question, thanks for letting me know.

avatar image BrodyDaChappy BrodyDaChappy · Jul 13, 2016 at 06:32 PM 0
Share

https://play.google.com/store/apps/details?id=com.HappyChappyGames.Bounce_E&hl=en_GB

Link to my game on android, $$anonymous$$us the new art work, added in next update.

Show more comments

0 Replies

· Add your reply
  • Sort: 

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

86 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

Related Questions

Hi thanks in advance , i can't find the error in my code but my character can't stop from going up when i jump onto the platform i made which has a platform effector 2D applied to it 0 Answers

Gradually rotate to slope angle 3 Answers

Making a level for a 2D platform Racing game? 0 Answers

how to change object's B color when object A is in range? 2D 1 Answer

Should i use rigidbody for platformer movement? 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