- Home /
Mouse should Check Collison of object and change its color
Hello Guys, I've been working on my game where i have 2 blocks and if i click one block with mouse that block should change its color but both blocks are changing their color.I have placed them in Default Layer and Also Added Collison both Mesh and Box. i also Tried With Mesh Collider Active and Box Collider Active Separately.But it Did not Work please Help.I code which i tried is: void Update() { if (Input.GetMouseButtonDown(0)) { //Statements; } }
Answer by Glurth · Oct 22, 2016 at 04:47 PM
It looks like you have not programmed all the conditionals you need:
You do check if the mouse button is down, but you do NOT check the location of the mouse.
You will need to either: check the mouse location on click, and see if that is "on" one of your blocks (and if so, which one). Then you can highlight only that block.
(you'll need this function- https://docs.unity3d.com/ScriptReference/Camera.ScreenPointToRay.html, and some knowledge of raycasting: https://unity3d.com/learn/tutorials/topics/physics/raycasting)
OR
setup a trigger on each block, that is automatically called by the unity event system on click. (you'll need to learn about callbacks and event system stuff- I suspect this interface will be useful: https://docs.unity3d.com/ScriptReference/EventSystems.IPointerDownHandler.html)
Your answer
Follow this Question
Related Questions
choppy mouse in webGL build 0 Answers
have object rotate to the mouses screen position 1 Answer
Fix Gimbal Lock Mouse Look? 0 Answers
Camera problems 1 Answer
draw line between mouse and gameobject on flat plane 0 Answers