Normalize Vector3.Distance based on rotation - unity3d

I am trying to measure distance between multiple positions but I do not want the rotation to affect the distance. In concept, I want to track the starting transform and upon each update track the distance traveled without regard to the change in rotation. I am using an HTC Vive controller and people tend to rotate their hands and I want to control for this.
I've tried resetting the Eular Angles, but this doesn't seem to work.
Adding an Analogy that will certainly help.
Think of it like trying to draw and measure a line with a pencil, the position is in the eraser, and I can hold the pencil in any number of ways and in fact change the position in the middle of drawing the line, but my line will remain straight and the measurement will remain accurate.
Any help is appreciated.

I believe your problem lies around the position you are tracking. It sounds like you are tracking the transform.position of one of the child elements of the Vive controller model, leading to the situation that you're describing with the pencil eraser analogy.
Depending on where your script is attached, you could either move this to the top level element of the Vive controller, or alter your script to instead track transform.parent.position, which shouldn't be affected by the rotations of someone's hand.

Related

Unity - How to disable animation interpolation / animation curves?

I'm trying to animate a hierarchy of 2D sprites (essentially body parts) by explicitly setting sprite positions at various key frames throughout a given animation clip. Unfortunately, Unity is implicitly changing all of the sprite positions using interpolation between key frames. This causes the sprites to look like they're sliding around rather than immediately transitioning into their correct positions.
So far I've come up with 2 rather poor solutions:
I could potentially create separate animation clips for each combination of sprite positions and transition
between them using mecanim parameters or in
code, but this seems tedious at best and inefficient at worst.
I could add more keyframes (either in the animator tab or in the
curves screen) that maintain each sprite position until just before
they need to be updated. This is a slightly better option but also
extremely tedious.
Is there any way to tell Unity to disable animation interpolation at least as far as positions are concerned? Thanks.
In the current version of Unity (2019.3), go to curves view, right click a keyframe node and select Left Tangent -> Constant. You may want to use Right Tangent depending on the use case. That should give you the instant change you're looking for.
Have you checked Brackeys video of animating 2D in Unity? Maybe it can help you :)
The following link is his video on how to animate 2D sprites.
https://www.youtube.com/watch?v=whzomFgjT50
Alright, I've come to the unfortunate conclusion that Unity forces you to use curves when dealing with animation keyframes and that you need to add an extra set of keyframes for abrupt shifts in animation.

How to make dotted physics shooter simulator like bubble shooter

I would like to achieve something like this:
I've already done something similar using the Unity particle system. It works fine but the problem is when you rotate the pointer, a wave is formed. Is there any other way?
If you want to avoid the wave using particle effects, you will need to move each particle immediately by how much it needs to move to get into its new position. The reason you get a wave now is because you're only changing the emitter's direction.
Instead, it might be a better approach to use a wrapped LineRenderer and scrolling the UVs on the texture/renderer on each Update using lineRenderer.material.SetTextureOffset. You'll also need to calculate the points to draw the line, and set those as appropriate in Update as well.
This way, when you change the aim, the LineRenderer's positions can all move accordingly and the dots will always appear to be in a series of straight lines.
Increase the speed of the Simulation Speed in the main (top) section of the Particle Settings. Try 10, to start with. You might need even higher. Default is 1.

How to make a perfect pinch zoom (Unity 3D)

I am searching for a solution to how to make a perfect pinch zoom in Unity by moving the camera along the forward:
Set up:
Horizontal plane centred at the origin with all Game objects.
Perspective camera with FOV 10, offset at (10,10,10) looking down at a 45 degrees angle, so that it looks at the origin (there is also a rotation of 45 degrees around the axis pointing up, to achieve this).
What I need:
When I place two fingers on the screen I am touching two GameObjects with them - so the screen coordinates under the fingers correspond to certain world coordinates. When I make a pinch movement (with moving two fingers or only one) I want the new screen coordinates to correspond to the same world coordinates that were under the fingers at the beginning of the whole interaction.
So to simplify even further - whenever I touch the screen with two fingers, I want the world coordinates corresponding to the screen coordinates under my fingers to always stay under the fingers (allowing a very small margin of error).
An example of this perfect zoom for which I am looking for you can see in the mobile game Boom Beach from Supercell.
I already tried to move the camera along its forward vector and to reposition it and I get pretty good results, but pretty much always the GameObjects underneath ‘slip’ away from under my fingers, that is at some points are no longer underneath them. It would be great if there was a mathematical solution to this, but if it’s necessary to compute the answer (through some search for example) then this is totally fine.
If the setup/scenario is not clear, I could provide some sketches to clarify it a bit more.
Hope someone can help me! :)
I would set up a system that detects when the user is zooming in and out if you are using GameObjects to pinpoint where the fingers are that is easy to do with Vector3.distance. After that, I would make a function that moves the camera closer to your desired zoom level with Vector3.MoveTowards(camera position, desired position, the speed of movement) where I would set "speed of movement" as a mathf.sqrt(vector3.distance(Camera position, Desired position));
as for the "desired position" I would set that Vector3(position) as a fraction of a line between two game objects that represent your maximum and minimum zoom level.
EDIT: with that, you should have a very nice camera system

Trouble getting correct height of joint positions in Unity Xbox

I am using Xbox-Unity and am trying to make a Kinect game. I need to be able to know when a player's foot is in the air and when it comes back down on the ground. I thought that this would be as simple as tracking the Joint Positions but the foot's Y changes based on the proximity to the Kinect Camera (Taking the foot joint position from Kinect). If I lifted my left foot up far away from the camera, it's Y would be high(let's say 10). If it were to land close to the camera, the Y would be low(let's say -20). What I had hoped was that I could just say 0 is the floor and have an easy time knowing when a foot was in the air and when it was on the ground. Does anybody have any ideas on how I can correctly tell when a foot is grounded?(everything I can think of so far had at least one exception that would make the gameplay broken)
Edit: Used a point to plane equation but no matter what I do, the distance to floor is always different based on my proximity to the camera.
One possibility would be to compare it to the other foot.. if one is higher than the other, chances are they're standing on the other foot. If you're looking to detect jumps, you should be able to find a sudden change in the y position of both feet.
There's also the Floor Clipping Plane, but that involves some more complicated math from what I've seen. Check out the Kinect programming guide, which is super old but I think should still be relevant here. The section "Floor Determination" is what you're after.

How to handle a game world that wraps, using cocos2d on the iPhone

I have a game world that's much bigger than the view port, the main character stays in the center of the view port at all times and the background layer is moved around to give the impression of the character moving. I want to make it so that the game world wraps, meaning if the the character keeps traveling either left or right they will eventually end up back at the starting position. There will be moving entities in the game world so the biggest problem I foresee is that if you go to the far right of the map you should be able to see any of the moving entities that are within the first small section of the far left of the map.
I've thought a bit about this and any solution I've come up with seems far too complicated. Like creating two identical game worlds side by side and moving them around accordingly. I live in hope that there is an elegant solution to this. Any expertise you can share would be greatly appreciated.
I'm using cocos2d on the iPhone just in case that makes any difference.
An example might be to have an x,y offset for your camera, and a multidimensional array of sprite objects.
As the player moves, the offset value changes, e.g., xMove = -1.4 and yMove = +2.6.
Then you would iterate and change the positions of all the tiles by that amount.
Next, you would identify the sprites that are too far away from the center of the screen (0,0) and re-position them to the opposite side, so they will always be visible.
This would all be done on the same scheduled 'tick' so no graphical artifacts occur.
I'm pretty surprised no one has made a wrappable tile map yet for cocos2d.
I can't give cocos2d specific advice, but I would say the most common way to do this is to create one game world, draw (parts of it) multiple times and make sure that your logic for things like collisions and AI checks for wrap-around where appropriate.
So if your player character is close to the corner of the world, you'd draw the world four times with different offsets. This needn't actually draw every single thing in the world four times any more than you would normally need to draw the entire world when only a small part of it is on-screen.