Checking for swipe of object in 3d space - c#

I'm writing a game just now in Unity. The basic premise is there are a few objects on screen in 3d space which you swipe to destroy. I've done most of the hard graft but got stuck on something I think should be fairly simple.
I've written a touch controller which spits out the start, end and direction of a swipe. How can I use this to check whether the swipe line in screen coordinates intersects an object in 3d space? I've looked into Rays but can't seem to get it working when casting from anywhere but the camera. My objects do have a collider and I attempted (briefly) to use the collider bounds too. Just can't seem to crack it!
Any help is appreciated,

use "Camera.ViewportPointToRay" to shoot rays depending on which pixels are swiped. Bottom-left of the screen is (0,0) top-right is (1,1).

Related

Moving character face to face the mouse, but without moving out of the head.? Unity2D

So I want to make a game with RPG movement, but it's more like a wave game, with static camera.
I want to move the character's face to make it look like it's looking at the mouse, but I didn't find how. I know how to rotate it but not how to rotate it with the mouse. Here are some images so I can explain better.
This will be when the mouse is either in 0,0 or outside the window
And this is when, for example, the mouse is in the right
You can get the coordinates of the mouse position in world space by using Camera.ScreenToWorldPoint. You could use the coordinates to find the right position for the eyes.

Walking up and down 2d slopes (pixel art)

I'm having an issue where I can't walk up slopes in unity. My art is pixel so I understand the slope is made with a gap of 1 pixel to create the actual slope, but I have no idea how to fix it so I can just walk up and down them normally. Currently, going down them makes it a little bouncy and going up them is impossible unless you jump. Any help would be appreciated! Here is what the slope looks like:
Edit: Collider looks like this but I don't know how to fix it:
Sprites automatically have a polygon collider created for them when imported into the project. This polygon collider drives the tilemap polygon collider shapes.
You can modify the physics shape for a sprite to smooth it out and remove this unwanted when going up a ramp. Custom Physics Shape documentation
Another important thing to note in your specific problem: Often when a character has a "box-like" shape, they will get snagged on edges and small collider deviations. Unless your game's playstyle is based around a box-shaped entity and interactions, it's usually recommended to use a rounded collider for the moving characters (like a 2d or 3d capsule collider).

Casting a ray from mouse through distortion matrix

I've searched the board, as well as the oculus board, and unity board. Couldn't really find something that helped.
I'm working on a vehicle simulation. Before we started using the oculus, it was just a regular first person perspective. You used a racing wheel/pedals to drive and the mouse to control all the buttons and switches etc. We use raycasting from the mouse point on the screen into the world to interact with the various controls in the vehicle.
Now that we're using the oculus, the raycast isn't taking into account the distortion matrix used on the oculus cameras. So you're not actually casting a ray at what you're visually clicking on. Using Debug.DrawRay I found that it was slightly off. Just to be sure, I disabled the lens correction via inspector on the OVRCameraController and sure enough the raycasting was working again.
The ray itself is calculated the usual way one does when firing from the mouse point:
ScreenPointToRay(Input.mousePosition);
Would anyone have any idea how I can adjust my ray so it works with lens correction on?
Cheers,
Gordon
Simply multiply the Distortion-Matrix with the Ray's Vectors (Position and Normal) and you have your new Ray. I would suggest using Homogeneous Coordinates with 4x4 Matrix and Vec4's where Positions has component w = 1.0 and Normals have w = 0.0; This way you can simply multiply and you are done - depending on lookup direction you might have to use the inverse matrix :)
Alright, what I ended up doing was creating a 3D cursor, bypassing the distortion matrix entirely.
I placed a gameobject at the same place as the "head" (between left eye and right eye cameras). It has a script on it that rotates up/down/left/right based on mouse movement. I then temporarily put a spot light with a narrow cone and high intensity on it so it looked like a laser pointer. I figured if the light is hitting things, so should a raycast of the same origin. Which ended up working.
However this didn't really solve the issue of using a cursor. I tried a number of things that ultimately didn't work (didn't line up with with where the light/raycast hit).
Finally I realized I was overlooking something very simple. I lowered the near clipping plane of the cameras and placed a plane as close as I could to the camera while still being visible. I then rotated it on local y by 180 so it would be invisible to the cameras and not block ray casts.
I then added some code so that when a raycast hit something, it would fire a second raycast from the hit point back to the origin. On the way it would have to hit the plane, which was essentially at the near clipping plane. I would then move my 3D cursor to that hit point.
Now it works as intended. Where the cursor is, is where the original raycast hit. The cursor now matched the position of the laser dot. So then I removed the light component. Done.
Hope this helps someone else someday.

Collision depending of side of the rectangle?

Im not intrested in the code for this problem i just want to be pointed in the right direction.
Im using C# XNA if that helps.
Basicaly for my game i am adding collisions for example the player can't walk or fall through stones.. I have the rectangle and i know i use the .intersects comand but would i need to check collision depending on the side of the rectangle here?
I know if the player is falling i can make if playerRectangle intersects stoneRectangle playerY = stopFalling..
But if the rectangle is coliding on the side of the player.. That would be different wouldn't it ?
One simple way of dealing with this is to work out the main axis of penetration, i.e. is the player further into the rectangle horizontally, or vertically. Then, using this information, you'd move the player so they were just touching the rectangle.
i.e. Player bumps into a wall on his right.
Main axis is Horizontal, and he's to the left of the centre of the rectangle, so we know to move him left.
Searching on gamedev.stackexchange.com will give you lots of different collision detection/response options.
If your game is 2D I would recommend using something like FarSeer Physics rather than implementing yourself.
If you are interested in the algorithms it is open source and you can poke around the code to see how they implemented collision detection.

Hiding objects that obscure the player in a 3D scene

I'm designing a 3D game with a camera not entirely unlike that in The Sims and I want to prevent the player character from being hidden behind objects, including walls, pillars and other objects.
One easy way to handle the walls case is to have them face inwards and not have an other side, but that won't cover the other cases at all.
What I had planned is to somehow check for objects that are "in front" of the player, relative to the camera, and hide them - be it by alpha blending or not rendering at all.
One probably not so good idea I had in mind is to scan from the camera to the player in a straight line and see if you hit a non-hidden object, continuing until you reach the player. Unfortunately, I am an almost complete newbie on 3D programming.
Demonstration SVG illustration < that wall panel obscures the player, so it must be hidden. Another unrelated and pretty much already solved problem is removing all three wall panels on that side, which is irrelevant to this question and only caused by the mapping system I came up with.
What I had planned is to somehow check for objects that are "in front" of the player, relative to the camera, and hide them - be it by alpha blending or not rendering at all.
This is a good plan. You'll want to incorporate some kind of bounding volume onto the player, so the entire player (plus a little extra) is visible at all times. Then, simply run the intersection algorithm for each corner of the bounding volume.
Finding which object is at a given point on screen is called picking. Here's an XNA link for you which should direct you to an example. The idea is that you retrieve the 3D point in the game from the 2D point, and then can use standard collision detection methods to work out which object is occupying that space. Then you can elect to render that object differently.
One hack which might suffice if you have trouble with the picking approach is to render the character once as part of the scene, and then render it again at the end at half-alpha on top of everything. That way you can see the whole character and the wall, though you won't see through the wall as such.
One easy way, at least for prototyping, would be to always draw the player after you draw the rest of the scene. This would ensure that the player is rendered on top of anything else in the scene. Crude but effective.
Create a bounding volume from the camera to the extents of the player, determine what objects intersect that volume, and then render them in whatever alternate style you want?
There might be some ultra-clever way to do this, but this seems like the pretty straightforward version, and shouldn't be too much of a perf hit (you're probably doing collision every frame anyway....)
The simplest thing I can think of that should work relatively well is to model all obstacles by a plane perpendicular to your ground (assuming you have a ground.) Roughly assuming everything that is an obstacle is a wall with some height.
Model your player as a point somewhere, and model your camera as another point. The line in 3d that connects these two points lies in a plane that is particularly interesting to you, because if this plane intersects an "obstacle plane" below the height of the obstacle, that means that that obstacle is blocking your view of the player point.
I hope thats somewhat clear. To make this into an algorithm you'd have to implement a general method for determining where two planes intersect (to determine if the obstacle is tall enough to block view.)

Categories

Resources