- Home /
Right Click to Ignore All But Terrain, How?
I have an RTS where the left mouse button is used for ID and information sorts of things and the right mouse button is used exclusively for movement (move to click point). I have no problems with controlling the left button by ignoring certain colliders etc. But I often need the right mouse button to ignore everything except the terrain so I can move objects to locations that is covered by other items. I'm currently looking at "layers" in the manual https://docs.unity3d.com/Manual/LayerBasedCollision.html. It's a bit confusing though. I don't need anyone to do my work for me but is there some other place(s) I can look that might assist me with my problem.
Answer by Nivbot · Mar 09, 2019 at 06:06 AM
Just give all your terrain a “terrain” tag and in your right click code add something like... i’m not at my computer so I’m not going to write actual code but anyway, something like...
If Button click right button Do raycast If raycast hit != null then If(hit.CompareTag(“terrain”)){ Only hit thing with terrain tag so do stuff }
I also forgot, you say you’re moving stuff? You can also add layers to ignore with the above or by itself.
You can use a Layer$$anonymous$$ask in your raycast to ignore or only hit certain layers.
For instance: playerLayer = ~(1 << Layer$$anonymous$$ask.NameToLayer("player") | 1 << Layer$$anonymous$$ask.NameToLayer("feet") | 1 << Layer$$anonymous$$ask.NameToLayer("enemy"));
That will ignore layers, player, feet and enemy.
If you want it to only want to hit a certain layer, leave off the ~ If I remember correctly.
This is a bit confusing to me. (Not your answer -- I'm confused by layers in general.) I've been reading material about layers and watching some videos about layers but I still don't have a good grasp on it just yet. Before I really go further, I need to write up a totally new program and dedicate it to layers and so forth so I can understand what is really happening. I'm going to do that right now but it will likely be some time before I "get it" and can move on. I think what you are suggesting is exactly what I should do but I need to do some experiments before I can accept your answer. Again, thanks.
Ya, thanks. Sounds like it will work. I'll try it here soon and if it works I'll "accept" your answer. I appreciate that you took the time to help me. I tried some stuff with "layers" but I couldn't get it to work. But, I didn't do it the way you suggest in your answer but it sure sounds like it will work. Thank you kindly for taking the time to help me.