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 alexjw90 · May 12, 2015 at 09:43 AM · unity 5raycastmeshmousepositiontile

Unity 5 - Hex Tile change color on mouseover

I am trying to change the color of the tile I made on mouse over but cant seem to get this working. I was able to make this code work with a plane but when i create my own mesh tiles, it doesn't seem to work. I'm quite new to unity so i'm not sure why the plane would work and this created mesh would not as they both appear to be the same makeup. This code can be placed on an empty object.

Hex Tile Creation:

 [ExecuteInEditMode]
 //Checks if they exist at runtime
 [RequireComponent(typeof(MeshFilter))]
 [RequireComponent(typeof(MeshRenderer))]
 [RequireComponent(typeof(MeshCollider))]
 public class TileMap : MonoBehaviour {
 
     //basic hexagon mesh making
     public Vector3[] vertices;
     public int[] triangles;
     public Vector3[] normals;
     public Vector2[] uv;
 
     // Use this for initialization
     void Start () {
         BuildMesh();
     }
     
     // Update is called once per frame
     void BuildMesh() {
 
         vertices = new Vector3[6];
         triangles = new int[4 * 3];    // Hex is made up of 4 triangles with 3 verticies per triangle
         normals = new Vector3[6];   //6 points that raise or lower
 
         // Generate mesh data
         float floorLevel = 0;
         vertices = new Vector3[]    //6 points in a hex
         {
             new Vector3(-1f , -.5f, floorLevel),
             new Vector3(-1f, .5f, floorLevel),
             new Vector3(0f, 1f, floorLevel),
             new Vector3(1f, .5f, floorLevel),
             new Vector3(1f, -.5f, floorLevel),
             new Vector3(0f, -1f, floorLevel)
         };
 
         triangles = new int[] // 12 = 4 triangles * 3 verticies per triangle
         {
             1,5,0,
             1,4,5,
             1,2,4,
             2,3,4
         };
 
         normals[0] = Vector3.up;
         normals[1] = Vector3.up;
         normals[2] = Vector3.up;
         normals[3] = Vector3.up;
         normals[4] = Vector3.up;
         normals[5] = Vector3.up;
 
         uv = new Vector2[]
         {
             new Vector2(0,0.25f),
             new Vector2(0,0.75f),
             new Vector2(0.5f,1),
             new Vector2(1,0.75f),
             new Vector2(1,0.25f),
             new Vector2(0.5f,0),
         };
 
 
         // Create new mesh and populate data
         Mesh mesh = new Mesh();
         mesh.vertices = vertices;
         mesh.triangles = triangles;
         mesh.normals = normals;
         mesh.uv = uv;
 
         // Assign mesh to filter/renderer/collider
         MeshFilter meshFilter = GetComponent<MeshFilter>();
         MeshRenderer meshRenderer = GetComponent<MeshRenderer>();
         MeshCollider meshCollider = GetComponent<MeshCollider>();
 
         meshFilter.mesh = mesh;
     }
 }


Color Hex Tile:

 public class TileMouseOver : MonoBehaviour
 {
 
     public Color highlightColor;
     Color normalColor;
 
     void Start()
     {
         normalColor = GetComponent<Renderer>().material.color;
     }
 
     void Update()
     {
         Camera mainCamera = Camera.main;
         Collider collider = GetComponent<Collider>();
         Renderer renderer = GetComponent<Renderer>();
         RaycastHit hitInfo;
 
         Ray ray = mainCamera.ScreenPointToRay(Input.mousePosition);
 
         if (collider.Raycast(ray, out hitInfo, Mathf.Infinity))
         {
             Debug.Log("In");
             renderer.material.color = highlightColor;
         }
         else
         {
             renderer.material.color = normalColor;
         }
 
     }
 }
 
Comment
Add comment · Show 1
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 alexjw90 · May 12, 2015 at 01:11 PM 0
Share

Turns out that i didn't have the the mesh attached to the mesh collider.

 // Create new mesh and populate data
          $$anonymous$$esh mesh = new $$anonymous$$esh();
          mesh.vertices = vertices;
          mesh.triangles = triangles;
          mesh.normals = normals;
          mesh.uv = uv;
  
          // Assign mesh to filter/renderer/collider
          $$anonymous$$eshFilter meshFilter = GetComponent<$$anonymous$$eshFilter>();
          $$anonymous$$eshRenderer meshRenderer = GetComponent<$$anonymous$$eshRenderer>();
          $$anonymous$$eshCollider meshCollider = GetComponent<$$anonymous$$eshCollider>();
  
          meshFilter.mesh = mesh;
   ------>meshCollider.shared$$anonymous$$esh = mesh; //allows raycast to recognize this

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

Detecting a mouse click on a custom 2D mesh? 1 Answer

Procedurally Connected Mesh/3d Model Tiles? 1 Answer

Creating a custom mesh - Cleaning the mesh failed 2 Answers

What is error on Raycasting? 0 Answers

Detecting whole gameobject, not just its collider 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