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.
Related
I am building a 3d topdown shooter. The player controls the avatar with the keyboard and the reticule with the mouse.
I found a simple way to implement the reticule based on this article:
https://gamedevbeginner.com/how-to-convert-the-mouse-position-to-world-space-in-unity-2d-3d/
I defined an object which represents the reticule and attached this script:
public class Reticule : MonoBehaviour
{
Camera mainCamera;
Plane plane;
float distance;
Ray ray;
// Start is called before the first frame update
void Start()
{
mainCamera = Camera.main;
plane = new Plane(Vector3.up, 0);
// This would be turned off in the game. I set to on here, to allow me to see the cursor
Cursor.visible = true;
}
// Update is called once per frame
void Update()
{
ray = mainCamera.ScreenPointToRay(Input.mousePosition);
if (plane.Raycast(ray, out distance))
{
transform.position = ray.GetPoint(distance);
}
}
}
This works, but the issue is that the reticule is lagging behind the mouse cursor, and catches up when I stop moving the mouse.
Is this because this method is just slow. Is there another simple way to achieve the same result?
the issue is that the reticule is lagging behind the mouse cursor, and catches up when I stop moving the mouse
That's normal, your mouse cursor is drawn by the graphics driver (with the help of WDM) as fast as the mouse data information comes over the wire, while your game only renders at a fixed framerate (or slower). Your hardware mouse will always be ahead of where your game draws it or anything related to it.
Some things that can help with working around this:
Don't show the system cursor, instead show your own. That way the cursor you show will always be in the same place your game thinks it is (since it drew it) and you won't have this issue. Careful with this however, because if your game's frame rate starts dipping it will be VERY noticeable when your cursor movement isn't smooth anymore.
Don't tie objects to your cursor. The issue doesn't show with normal interactions, like clicking buttons. You will notice this in RTS games when drawing boxes around units, but I struggle to think of another example of this.
Like above, but less restrictive, you could lerp the objects tied to your cursor in place, so they're always and intentionally behind it. It makes it look more gamey, which isn't that bad for a, you know, game. Though I wouldn't do this for a twitchy shooter, of course.
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.
I created a new empty gameobject in the hierarchy.
Attached to it a Box Collider:
And changed this settings on the Box Collider:
I set it's center property to 0,0,0
And set the size to x = 500 y = 600 z = 500
I also set the IsTrigger to be on( the checkbox is checked ).
And this is the Terrian details:
Width 500 Length 500 Height 600
When i'm looking at the scene window it seems like the box is around the terrain edges as it should be: ( Maybe in the right side there some space between the box and the terrain ? )
Scene Screenshot
This is the script attached to the empty gameobject ( InvisibleWalls ).
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class BoxCollider : MonoBehaviour {
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
}
void OnTriggerExit(Collider c)
{
Destroy(c.gameObject);
}
}
This is just for the test.
I added a break point on the line:
Destroy(c.gameObject);
Just to check when the event trigger.
The player ( ThirdPersonController ) is walking at speed 10.
When it's getting to the terrain edge the event is not triggering. The player keep walking on the air for some more seconds and only then the event is trigger and stop on the break point but then the player is already out the terrain area.
Event Triggered
What i want to do is once the player is touching the wall trigger the event and do something for example keep the player walking on place so the wall is blocking him. But now the event is trigger when the player is out of the terrain area.
Well this looks like the result of starting the player inside the collider, and using OnTriggerExit. That method won't be called until all of the player's collider is outside the defined box.
You could make the box smaller, so that there is some "padding" terrain around the outside of it. This is probably the quickest, but I would recommend option 2 instead.
You could make 4 box colliders, one for each wall. Instead of having one gigantic collider and waiting for the player to step out, think of these as skinny walls placed one to each side of your terrain. Don't make them triggers, and use the OnCollisionEnter event instead. This will automatically stop the player if you are moving him via physics, and you get the event as well. You can still make them triggers and use OnTriggerEnter if you want, but why not use the physics system since its there!
I hope this helps, good luck with your project!
Edit: Found a video that explains method 2
1) You Can call OnTriggerEnter if you want the player to be detected before it crosses the Trigger.
2) Another way will be To make the triggers Colliders and they will automatically block the player to cross the terrain boundary
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
How do I make so when I move the camera(with touch) it doesn't go beyond the scene borders?
How do I move the camera with touch so it moves strictly with scene parts, like slides(swipe-first slide, another swipe-another slide) with not going beyond the borders of the scene?
The game I'm making has a camera like in Disco Zoo game for android (I'm a newbie)
Technically, the scene doesn't really have a border. You'll need to define the border somehow within your game, then constrain the camera's position with something like Mathf.Clamp(value, min, max) in an Update() function on the camera.
How can you define the border? It's up to you. Some ideas:
Hard-code the values in the script that clamps the camera. Probably the quickest option, but not flexible
Make public parameters on the camera script that let you set min and max positions in the X and Y directions
If you have a background image: use the extents of that to define your camera's extents
Create empty objects in your scene that define the minimum and maximum extents of the scene. Put your "min" object at the top-left, and the "max" object at the top-right. Connect it to the camera script, then use those positions to see if you've gone too far in any given direction. The main reason to do this is that it's visual.
(Slower, but dynamic) If everything in your scene uses physics, you could search the entire scene for every Collider component, then find the furthest extents in each direction. However, this is probably going to be pretty slow (so you'll only want to do it once), it'll take a while to code, and you'll probably want to tweak the boundaries by hand anyway.