Unity 2D - Detect Tiles with Mouse - c#

Overview: I am making a 2D Tower Defense game with using Tilemaps as the environment and sprites as the towers and enemies. The mouse needs
Problem: I want to trigger different events/methods depending on which tile or sprite the mouse is hovering or clicking on.
Example - Hovering over buildable tiles shows a highlighted tile, but the highlight disappears when the mouse is over a "dead" un-buildable tile.
After 10+ hours of research, I think I need to use Raycasts, 2D Colliders, and Layers to detect when the mouse hovers over or clicks on a tile/object, but I don't know how to trigger different events/methods depending on which specific layer or tag the mouse is interacting with.
Question: How do I detect and access a tile or gameobject with a mouse hover/click? and is there a way to trigger different methods depending on tags/layers I assign to the things I want to detect on hover/click?

First of all you don't need raycasts or colliders to detect mouse events. There are many ways to do this, but you can create eventListener for use on multiple objects. Also as a simple solution you can consider using scripts(includes UnityEventListeners) attached to the objects you want to control.

Related

Unity movable Menu interface. Problem with collision detection

I assume this is pretty easy but I did not find a solution yet. I have a 3D environment for VR in Unity. I have a Canvas in Worldspace which you can grab and move around like a tablet. In order to be able to touch the buttons on the canvas, I added a sphere on the tip of the controller's finger as a detector with a sphere collider. To the buttons on the canvas I added rigidboys and a boxcollider. With a onCollisionEnter Event I handle if the button and the finger are colliding and execute the onclick.Invoke() function.
It is working, the problem is if I touch the buttons they start moving and float around in the environment. If I freeze their position they won't be moved if I drag the whole menu around. How could I solve this?
Using Unity 2019.4.19f1 with Oculus OVR Plugin.
Ok, I solved it with the idea of setting them to kinematic as JustARandomWibuuuu suggested. If someone might have the same problem.
I've set both to kinematic and in Project Settings → Physics → Contacts Pair Mode I had to enable all contacts.
You added rigidbody, so it will be a physical button, it will be affected by gravity, force, drag, ...., So it is floating because it is in 3D. You can use static rigidbody to do it
Just click true on
Rigidbody.isKineMatic

Unity drag and drop check right or left (2D)

I wanna build a card game in Unity.
I want to player swipe cards left or right but I wanna show some tricks about what's gonna happen if they swipe left or right so if they grab the card (2D Image) and go a bit left or right a text will appear on image depends on side.
I tried to use swipe and use X axis change but It's not working properly.
Here is a basic pictured information about what i wanna build:
I would advise using the drag handlers system in unity to track where the card started, and trigger different effects at different distances / directions from its origin.
You can detect the drag of the card by implementing IBeginDragHandler, IDragHandler, IEndDragHandler on a script on your card object.
Begin is called when the object starts dragging, Drag is called during each frame its moving, and End is called when the object is dropped.
When a drag begins, you can cache the position where the card started. Then in Drag handler each frame you can check the distance of your card from where it started. Once it has moved more than a certain distance, you can trigger whatever tricks or effects you want to show.
Depending on how your coordinate space is set up, you could for example know left from right during drag updates such as:
// Inside of your on drag method, check each frame for where the card is
// If it has moved far enough, trigger an effect
if (Vector3.Distance(dragStartPosition, transfom.position) > yourTriggerDistance) {
// if X is greater, the card moved right
if (transform.position.x > dragStartPosition.x) {
// Trigger Right swipe text
ShowSomeRightSwipeText();
}
// otherwise, X is smaller, so the card has moved left by the required distance
else {
// Trigger left swipe text
ShowSomeLeftSwipeText();
}
}
Also please remember that:
In order for these interfaces to work, you need to have a Physics 2D Raycaster attached to your camera in the scene.
You need to make sure you have an Event System in the scene as well.
The above two things are often asked issues here regarding detecting object dragging.

How to improve performance of rendering 3D scene using HelixViewport

I work on 3D project in C# and WPF I use Helix-Toolkit to show different 3D objects. I use Spheres a lot and I have also 3D text in the scene.
The problem is performance, for instance I on each mousemove I calculate the positions of each text in the scene but as the number of text increase the performance decrease.
I also have a Slider control to change spheres radius for each slider value as the user moves slider, this is also a problem of performance.
I do not know what to do is there any way to render the scene when all visual objects are changed their values because I think now the problem is that for each changed text position the 3D scene renders automatically its content.
First, Helixtoolkit.Wpf is using WPF internal 3D engine, all billboards/points/lines are drawn using CPU. If you have many billboards, you will experience performance hit very quickly. Try to use Helixtoolkit.Wpf.SharpDX if possible.
Mouse move has 100Hz frequency in WPF, you can try to only calculate the position every 2 move event or 3 move event to decrease the update rate.
I am not sure how you update your sphere size, usually you need to only update the transform to scale your sphere instead of creating a new sphere mesh each time.

VR score text display

I am trying to make a VR game with google cardboard in unity. However we can not find a way to display score text right in front of the player. However when I add 2D text it is only on one side and therefore on one side of the eye and getting the position right for 2 texts is hard. If I use 3D text and set in front of the players position I think it will go into the wall if a player hits one. Is their any way to display a score on google cardboard / Unity VR.
You can either use native Unity Canvas UI or Googles hack to render OnGUI calls onto a texture.
I would definately recommend Canvas as that is the way Unity is working on their UI features, and it has much better layout capability.
To use canvas, Right click in the hierarchy and add UI->Text. You will automatically get a canvas. The important part is set the canvas to world-space (not screen space overlay). Then drag the canvas game object so it is a child of the Google Cardboard Main head object. Scale it down (like x:0.001,y:0.001,z=0.001) because by default it will be massive. To avoid going through walls position it about 0.5m in front of the camera - within any collider you may have.
there is another approach as well which you can place that canvas under camera make it world space, and then adjust it as you want, after that where ever you look at it will be seen easily ( as suggested by earlier user in answers) as you can see below in picture i placed canvas > Text > under camera which i used for oculus/ google camera

Dynamically Create and Destroy PictureBoxes

I am trying to make a simple tile-based turn based strategy-game.
So I made a test-map with different things like trees, rivers etc.
My next step is to make classes that I call Unit, that holds the X-Y position and other data. Since Unit will be created/destroyed dynamically during the game (in a List>), I also want PictureBox containing the Unit tile-icon to be created/destroyed as needed.
I want to be able to click on a PictureBox, the box will make a mouse event, the event will check which class is connected to the PictureBoxand perform action as needed.
Dynamically Create and Destroy PictureBox in C#? Or should I make a grid of PictureBox?
I would go a different route. Create one single PictureBox and draw everything in it's Paint event. Do a nested loop through your map's x and y coords for the visible tiles and draw out those tiles using Graphics.Draw commands. After you paint the background tiles you can loop though the unit list and paint those right on top. You can do a simple hit-test on mouse down to see what is being clicked on.
This will be less resource intensive and also allow you to do better animations and graphics later (think sprites crossing tile boundaries or tiles larger than a single physical tile).

Categories