Create a 3d shape (non GUI) with a start and end point - unity3d

I know the traditional way in Unity to position a shape is to give it a center point, then scale and rotate as needed. However, I'd like for the following:
The player can click once to "start" a shape (basically a line, but I could use a cube or whatever) and then click a second time to "end" the shape, completing it and bringing it into existence in the game space. How could this be done?

Related

How to smoothly move a node in an ARkit Scene View based off device motion?

Swift beginner struggling with moving a scene node in ARkit in response to the device motion.
What I want to achieve is: First detect the floor plane, then place a sphere on the floor. From that point onwards depending on the movement of the device, I want to move the sphere along its x and z axis to move it around the floor of the room. (The sphere once created needs to be in the center of the device screen and locked to that view)
So far I can detect the floor and place a node no problem. I can use device motion to obtain the device attitude (pitch, roll and yaw) but how to translate these values into meaningful x, y, z positions that I can update my node with?
Are there any formulas or methods that are used to calculate such information or is this the wrong approach? I would appreciate a link to some info or an explanation of how to go about this. Also I am unsure how to ensure the node would be always at the center of the device screen.
so, as far as I understood you want to have a following workflow:
Step 1. You create a sphere on a plane (which is already done)
Step 2. Move the sphere with respect to the camera's horizontal plane (i.e. along its x and z axis to move it around the floor of the room depending on the movement of the device)
Assuming that the Step 1 is done, what you can do:
Get the position of the camera and the sphere
This should be first called within the function that is invoked after sphere creation (be it a tapGestureRecognizer(), touchesBegan(), etc.).
You can do it by calling position property of SCNNode for sphere and for camera position and/or orientation by calling sceneView.session.currentFrame's .camera.transform which contains all necessary parameters about current position of the camera
Move the sphere as camera moves
Having the sphere position on the Scene and the transformation matrix of the camera, you can find the distance relation between them. Here you can find a good explanation of how exactly you can do it
After you get those things you should implement a proper logic within renderer(_:didUpdate:for:) to obtain continuous lock of the ball with respect to the camera position
If you are interested about the math behind it, you can kick off by reading more about transformation matrices which is a big part of Image Processing and many other areas
Hope that this will help!

How to find the center in unity 2d?

I want to create 2d game. The game is similar to the Scale Puzzle. I've already created nearly all the functionality. Only the last remains.
.
This is example.
And that's how I draw shapes.
.
I click inside a white square, and after 1 seconds a square is drawn regardless of the position of the ball. (x,y).
Square is created programmatically, and it is added to the parent element "SquaresList" with name New Game Object.
How can i do, so that the violet field becomes larger, and in the middle of the screen.
I made it so that for every 3 clicks, "SquaresList" increases Scale by 0.25f, and get negative position of the ball. Example:
SquareList.transform.position = new Vector2(-ball.pos.x, -ball.pos.y)
This does not work correctly.
What options can be? (intersections, find the max/min point, math formulas) ?
Hi NoName :) Your method
SquareList.transform.position = new Vector2(-ball.pos.x, -ball.pos.y)
would work perfectly if your object does not inherit other objects and there positions. To fix this make sure that you reset the position of all other game objects that you use as containers.

Preventing an object from landing on its side; ensuring it always lands face-up or face-down

Currently I am doing a project of shell toss in Unity. The shell is made in Blender and imported in Unity. I attached a single box collider to the shell.
Now when I apply the random force to the shell to flip it, it sometimes rests in the vertical up position when either a result of heads (face-up) or tails (face-down) should have occurred.
I have tried changing the axis, increasing the gravity. But none of these solutions worked.
What I have done for now is when the shell is in a resting position after it lands, I rotate the shell 90 degrees - but this is not a great solution, since it is slow and takes time to check whether the shell has come to rest or not.
I am looking for the better idea so that the shell should only rests in a heads or tails state.
If you use a box collider for your shells, you are going to end up in this problematic situation where the flipped shell sometimes ends up sitting sideways, instead of face-up or face-down.
In this situation, I would recommend making use of Capsule Colliders, which are cylindrical colliders with a rounded top and bottom. If a capsule collider lands on either end, it'll fall over sideways:
However, one capsule collider isn't enough - otherwise, the shell will start rolling around after it falls over. I suggest 2 or 3, oriented in a cross such that their tips correspond to the sides of the shell:
Now, if the shell lands on any of its sides, it will topple over onto one of the faces of the cross - either face-up, or face-down. To add capsule colliders to your GameObjects, just click on Add Component, and select Physics > Capsule Collider. Then modify the properties to get them into the desired positions:
The Direction you choose should probably be X-Axis and Z-Axis. Once you have the correct orientation, change the Center value to move the colliders to the right positions to make a cross through the shell. Then, alter the Radius of the colliders to affect the thickness of the cross, and the Height so their tips line up with the edges of the shell.
Hope this helps! Let me know if you have any questions.

Lightning effect in opengl es

Is there a way to create a lightning effect on the iPhone using opengl?(like this app)
Right now I have modified the glpaint sample to draw random points around a line (between two points that the user touches) and then connecting them, but the result is a zigzag line that constantly jumps around and lags horribly on the actual device.
you'll probably just want to make a triangle strip from the center of the device to the point that is being touched, then apply a drawn lightning texture to that resultant polygon.
You can animate the texture in order to get the jumping lightning effect.
A simple way to create a lightning effect is to compute the lightning path using a 2D Perlin function, rendering it to a glow buffer, blurring it with a Gaussian blur shader, and merging it with the scene. You can make the lightning move by computing two paths (start and end) with an identical number of path nodes and moving each node of the start path successively towards the corresponding node of the end path. Once the end path has been reached, it becomes the start path and a new end path is computed.

iphone cocoa : how to drag an image along a path

I am trying to figure out how can you drag an image while constraining its movement along a certain path.
I tried several tricks including animation along a path, but couldn't get the animation to play and pause and play backwards - so that seems out of the question.
Any ideas ? anyone ?
What you're basically trying to do is match finger movement to a 'translation' transition.
As the user touches down and starts to move their finger you want to use the current touch point value to create a translation transform which you apply to your UIImageView. Here's how you would do it:
On touch down, save the imageview's starting x,y position.
On move, calculate the delta from old point to new one. This is where you can clamp the values. So you can ignore, say, the y change and only use the x deltas. This means that the image will only move left to right. If you ignore the x and use y, then it only moves up and down.
Once you have the 'new' calculated/clamped x,y values, use it to create a new transform using CGAffineTransformMakeTranslation(x, y). Assign this transform to the UIImageView. The image moves to that place.
Once the finger lifts, figure out the delta from the original starting x,y, point and the lift-off point, then adjust the ImageView's bounds and reset the transform to CGAffineTransformIdentity. This doesn't move the object, but it sets it so subsequent accesses to the ImageView use the actual position and don't have to keep adjusting for transforms.
Moving along on a grid is easy too. Just round out the x,y values in step 2 so they're a multiple of the grid size (i.e. round out to every 10 pixel) before you pass it on to make the translation transform.
If you want to make it extra smooth, surround the code where you assign the transition with UIView animation blocks. Mess around with the easing and timing settings. The image should drag behind a bit but smoothly 'rubber-band' from one touch point to the next.
See this Sample Code : Move Me