Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 GabboUnity · Sep 27, 2014 at 03:00 PM · touchspherecheck

Check planes that have the same color of gameobject

Hi guys! In my game I have a sphere that rolls on some planes that are created and colored randomly and I want to check when the planes have got the same color of the ball (that is also colored randomly) because I want that when the ball is on a plane that has got its same color, the player must click on the screen to increase the score and to change the color of the sphere. Then, obviously, if the player doesn't click on the screen when the sphere rolls on the first plane that have its same color the game quit. How can I do? Now I write the script that I have to create random planes and to change the color of the sphere after touching screen. Thanks a lot!

SCRIPT TO CREATE RANDOM PLANES.

 #pragma strict
  
 import System.Collections.Generic;
  
 var list : List. = new List.();
  
 var s : GameObject; 
 var colors = [Color.red, Color.green, Color.blue, Color.yellow, Color.white];
  
 function Start () { StartCoroutine(creazione(0.000000000001)); }
  
 function creazione(tempo) {
  
      while(true) {
         var pos : Vector3;
         for (var i = 0; i < 30; i++) {
             pos = Vector3(0f,0f,Random.Range(0,100000));
             if (!CheckOverlap(pos)) break;
         }
         if (CheckOverlap(pos)) break;
  
         s = GameObject.CreatePrimitive(PrimitiveType.Plane);
         s.renderer.material.color = colors[Random.Range(0, colors.Length)];
         s.transform.position = pos;
         list.Add(s.transform);
         yield WaitForSeconds(tempo);
     }
 }
  
 function CheckOverlap(pos : Vector3) {
     for (var t : Transform in list) {
         if (Vector3.Distance(t.position, pos) < 10.0)
             return true;
     }
     return false;
 }   
 
 SCRIPT TO CHANGE THE COLOR OF THE SPHERE.
 
 var sphere : GameObject;
         var colors = [Color.red, Color.green, Color.blue, Color.yellow, Color.white];
 
 function Update() {
     if (Input.touchCount == 1) {
             sphere.renderer.material.color = colors[Random.Range(0, colors.Length)];
     }
 }
     


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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by robertbu · Sep 27, 2014 at 03:16 PM

Here is a bit of untested code that Raycasts() down from the center of an object and find and tests the color against the current object:

 var hit : RaycastHit;
 
 if (Physics.Raycast(transform.position, Vector3.down, hit)) {
     var rend = hit.collider.renderer;
     if (rend != null) {
         if (rend.material.color == renderer.material.color) {
             // Do something
         }
     }
 }

Note I use a direct comparison operator ('==') to test the two colors. I'm not sure how Unity handles comparisons of colors. If two colors that appear to be the same you can check with Debug.Log()) are not considered equal, you can check the color distance instead.

 function ColorSqrDist(c1 : Color, c2: Color) : float {
     return (c2.r - c1.r) * (c2.r - c1.r) + (c2.g - c1.g) * (c2.g - c1.g) + (c2.b - c1.b) * (c2.b - c1.b));
 }
Comment
Add comment · Show 12 · 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 GabboUnity · Sep 27, 2014 at 03:38 PM 0
Share

But this script works only with real collisions? I write this script: var hit : RaycastHit; var sphere : GameObject; var colors = [Color.red, Color.green, Color.blue, Color.yellow, Color.white];

if (Physics.Raycast(transform.position, Vector3.down, hit)) { var rend = hit.collider.renderer; if (rend != null) { if (rend.material.color == renderer.material.color) { sphere.renderer.material.color = colors[Random.Range(0, colors.Length)]; } } } and I attached it on the sphere, but nothing happens..

avatar image robertbu · Sep 27, 2014 at 04:11 PM 0
Share

You need to debug it. It will only work if the planes have a mesh collider, and if the first object below your ball is the plane. Put a Debug.Log() statement just inside the 'if' statement to see if it is hitting something, and if so what:

  Debug.Log(hit.collider.name + ", " + hit.collider.tag);

If it is hitting something, then just before doing the comparison, do:

 Debug.Log(rend.material.color + ", " + renderer.material.color);
avatar image GabboUnity · Sep 27, 2014 at 05:34 PM 0
Share

I tried, but it seems that nothing happens. How can i be able to check same colors between the two colliders (ball and plane)?

avatar image robertbu · Sep 27, 2014 at 06:31 PM 0
Share

The above fragment goes in the Update() function of a script on the ball. Given a hit by the Raycast(), this:

   var rend = hit.collider.renderer;

...gets the renderer on the object hit and this:

  rend.material.color

...get the color. If you are absolutely sure that what get hit has a Renderer, you can do it in one step:

 var color = hit.collider.renderer.material.color;

I tried, but it seems that nothing happens.

First if I did not make it clear in may answer, the above is a code fragment that needs to go in Update(). That is, the Raycast() needs to happen each frame. If it is in Update(), then please post your current entire script on the ball, and I'll take a look to see what is going on.

avatar image GabboUnity · Sep 28, 2014 at 10:38 AM 0
Share

var hit : RaycastHit;

if (Physics.Raycast(transform.position, Vector3.down, hit)) { var rend = hit.collider.renderer; if (rend != null) { if (rend.material.color == renderer.material.color) { Debug.Log(hit.collider.name + ", " + hit.collider.tag); } } }

This is the script that I attached on my sphere..

Show more comments
avatar image
0

Answer by GabboUnity · Sep 29, 2014 at 12:58 PM

I wrote the new answer @robertbu at this URL: http://answers.unity3d.com/questions/799643/check-touch-on-mobile-screen-and-do-different-thin.html Look at it if you have anytime!

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.

Follow this Question

Answers Answers and Comments

2 People are following this question.

avatar image avatar image

Related Questions

Check for closed circuit of pickups 2 Answers

How to check if specific objects touch 0 Answers

How can I check if my Player leaves a Sphere? 2 Answers

Android touch is not dynamic 1 Answer

Collision between sambe objects with same Layer 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