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 $$anonymous$$ · Sep 27, 2016 at 02:05 AM · collisionchange color

change color before and during collision

i want to change color of object before and during collision. no problem changing it using OnCollisionEnter when two of the colliders collide. any advice on how to also change the color before objects collide each other? is it that I need to update the collider.contact.offset ?

pls help. thx in advance.

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
0
Best Answer

Answer by TBruce · Sep 27, 2016 at 03:40 AM

Lets say you have a cube. Now add an empty game object to the cube so that your hierarchy looks like this

Hierarchy

The Cube already has a BoxCollider on it. Start by copying the collider from the cube. You do this be selecting the little gear icon in the upper right corner of the BoxCollider and select Copy Component from the drop down menu.

Now go back to the empty game object that you created and select the little gear icon in the upper right corner of the transform and select Paste Component as New from the drop down menu.

Now go back to the cube and make that BoxCollider larger than the cube - make it large enough so it is at the point where you want the color to change before collision.

Note you need to check Is Trigger on all colliders (this will not work properly if not).

Now add this script to both the Cube and child object.

 using UnityEngine;
 using System.Collections;
  
 public class ColoredCube : MonoBehaviour
 {
     public Color newColor = new Color(1.0f, 1.0f, 1.0f, 1.0f); // change this to desired color
     public bool changeColor = true;                            // optional flag
     public string objectToCollideWith = "Block";               // used to determine object fo collision
     
     public Renderer renderer = null;
 
     void OnTriggerEnter(Collider col)
     { 
         if ((changeColor) && (col.transform.tag == objectToCollideWith) && (renderer != null))
         {
             renderer.material.color = newColor;
         }
     }
 }

Set all variables for each script on the cube and child object in the inspector (drag and drop the cube to the renderer variable in both scripts).

On the separate object that will be colliding with the cube it needs a collider (with Is Trigger checked) and it needs a Rigidbody (with Use Gravity un-checked).


inspector.png (1.7 kB)
Comment
Add comment · Show 2 · 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 $$anonymous$$ · Sep 27, 2016 at 05:16 AM 0
Share

thx for prompt reply.

tried but no changes. no collision and color change.

I tried similar way which works for me. - GameObject collider to be bigger than cube size and is trigger. - cube collider to be same size as cube. non-trigger. - target object collider to be non-trigger. - all must be in same layer.

this method works for me. object collides and changes color.

thx for replying. appreciate your help.

avatar image TBruce $$anonymous$$ · Sep 27, 2016 at 05:27 AM 1
Share

Please take a look at this Unity package to see a working version in action.

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

94 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Destroy GameObject inconsistent with Instantiated children 1 Answer

How to make a player teleport on collision? 2 Answers

How to move an object on a terrain that will always stay on top of the terrain? 2 Answers

How to play an animation on collision 1 Answer

Sphere bouncing back on edges of aligned objects 0 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