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 george_vasilchenko · Sep 26, 2014 at 01:52 AM · raycastarraymaterialselection

Make a selection

Hey guys, I am trying to make an application where u have an object that is made of several parts and u can orbit the camera around it. Moreover, its possible to select different parts and change their materials. But I am currently stuck with selection method.. I want to be able to hover over and see some material change like a temporary glow over possible selection part, and if I click, glow becomes permanent with a different color. I cant figure out.. Please help! Thank you!

PS. And maybe you can also advise how to loop through an array of the objects in order to find their materials, to be able to change them after the selection is over. My problem with efficiency :)

using UnityEngine; using System.Collections.Generic;

public class MouseEventHandling : MonoBehaviour {

 public GameObject roof;
 public GameObject body;
 public GameObject veranda;
 public GameObject basePart;
 public GameObject fence;
 public GameObject stairs;
 public GameObject door;
 public GameObject bladesL;
 public GameObject bladesFabric;
 public GameObject windowFrame;
 public GameObject windowGlass;

 public Material roofMat1;
 public Material roofMat2;
 public Material bodyMat1;
 public Material bodyMat2;
 public Material verandaMat1;
 public Material verandaMat2;
 public Material basePartMat;
 public Material fenceMat;
 public Material stairsMat;
 public Material doorMat;
 public Material bladesLMat;
 public Material bladesFabricMat;
 public Material windowFrameMat;
 public Material windowGlassMat;

 public Material selectionMat;

 public Light dirLight;

 public Color cameraBG;
 public Color lightColor;
 public Color beginColor = new Vector4(0, 0, 0, 1);
 public Color endColor = new Vector4(1, 1, 0, 1);

 public Color beginColor_sel = new Vector4(0, 0, 0, 1);
 public Color endColor_sel = new Vector4(0, 0, 1, 1);

 public RaycastHit hit;

 public float rayDistance = 500.0f;

 private bool isHit = false;
 public  bool isSelected = false;

 void Start()
 {
     roof.renderer.material = roofMat1;
     body.renderer.material = bodyMat1;
     veranda.renderer.material = verandaMat1;
     basePart.renderer.material = basePartMat;
     fence.renderer.material = fenceMat;
     stairs.renderer.material = stairsMat;
     door.renderer.material = doorMat;
     bladesL.renderer.material = bladesLMat;
     bladesFabric.renderer.material = bladesFabricMat;
     windowFrame.renderer.material = windowFrameMat;
     windowGlass.renderer.material = windowGlassMat;

 }

 void Update () 
 {
     ChangeMaterialOnHit(roof, roofMat1, selectionMat);
     ChangeMaterialOnHit(body, bodyMat1, selectionMat);
     ChangeMaterialOnHit(veranda, verandaMat1, selectionMat);
     ChangeMaterialOnHit(basePart, basePartMat, selectionMat);
     ChangeMaterialOnHit(fence, fenceMat, selectionMat);
     ChangeMaterialOnHit(stairs, stairsMat, selectionMat);
     ChangeMaterialOnHit(door, doorMat, selectionMat);
     ChangeMaterialOnHit(bladesL, bladesLMat, selectionMat);
     ChangeMaterialOnHit(bladesFabric, bladesFabricMat, selectionMat);
     ChangeMaterialOnHit(windowFrame, windowFrameMat, selectionMat);
     ChangeMaterialOnHit(windowGlass, windowGlassMat, selectionMat);

 }

 void ChangeMaterialOnHit(GameObject part, Material originalMat, Material selectionMat)
 {
     string name = part.name;

     Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);

     if(!isSelected)
     {
         if(Physics.Raycast(ray, out hit, rayDistance) && hit.collider.name == name)
         {
             part.renderer.material = HoverGlowMaterial(part);

             if (Input.GetMouseButtonDown(0)) 
             {
                 isSelected = true;
             }
         }
         else 
         {
             part.renderer.material = originalMat;
         }
     }
     if (isSelected) 
     {
         part.renderer.material = SelectedGlowMaterial(part);
     }



     Debug.DrawRay(ray.origin, ray.direction * rayDistance, Color.red);
 }

 Material HoverGlowMaterial(GameObject target)
 {
     float lerp = Mathf.PingPong(Time.time, 0.5f);
     Color newColor = Color.Lerp(beginColor, endColor, lerp);
     selectionMat.SetColor("_GlowColor", newColor);
     selectionMat.SetFloat("_GlowStrength", 1.5f);
     selectionMat.SetTexture("_MainTex", target.renderer.material.GetTexture("_MainTex"));
     return selectionMat;
 }
 Material SelectedGlowMaterial(GameObject target)
 {
     float lerp_sel = Mathf.PingPong(Time.time, 0.75f);
     Color newColor = Color.Lerp(beginColor_sel, endColor_sel, lerp_sel);
     selectionMat.SetColor("_GlowColor", newColor);
     selectionMat.SetFloat("_GlowStrength", 0.5f);
     selectionMat.SetTexture("_MainTex", target.renderer.material.GetTexture("_MainTex"));
     return selectionMat;
 }

}

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

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

2 People are following this question.

avatar image avatar image

Related Questions

How to change colour of Game objects when hit by ray cast using array 0 Answers

Boolean being overwritten 1 Answer

[SOLVED]Object selection scrip doesn't work. 2 Answers

Add two radius on the same shader 1 Answer

RTS Game: Unit selection through Raycast 2 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