How can I rotate an object based on an angle? - unity3d

I am working in Unity3D and I was wondering how I would rotate a cube based on the angle between the cube and the mouse position. With that I don't mean the mouse position in world space but in pixels.

Here are some pages that'll lead you to your answer:
Input.mousePosition This also includes an example of how to turn screen coordinates into a ray in world coordinates. If you know how far away from the camera you want your point, check out ScreenToWorldPoint for a point instead of a ray.
transform.Rotate To perform a rotation.
The rest of your question is kinda vague--rotating "based on" the angle between cube and mouse position could mean a lot of things. You shouldn't need much more information than this though!

Related

Unity 2D: Alter sprite anchor point

I've got a plain 2D Capsule Sprite. I want it to rotate around a specific point instead of its very centre. In other programs, this point is called the anchor point. For context, I'm making a paddle for my 2D pinball game so obviously the paddle needs to move when you press a button, but I can't have it moving around its very centre... Well, I could but I don't want to.
So my questions are:
Is anchor point the correct term for Unity?
Can this be altered/moved and how?
So, I ended up getting an idea from a friend who does this stuff too.
I made an empty game object. Made it the parent of my paddle. Moved the paddle to the point of the game object where I want the paddle to rotate around, and now when I rotate the game object, it rotates the paddle around the pivot point that I want it to have.
Image of paddle, arrow pointing to the empty game object
Edit: Just works using UI components within a canvas
You can use the Anchor presets of the Rect Transform. Or define the Min/Max values for x/y coordinates in Anchors Property.

Unity3d 5.6.0 How to transform the mesh

First of all, I'm a german so please forgive me my (maybe bad) English.
I'm programming a 2.5d Puzzle Jump'n'Run like Trine and of course, you can play an archer/ranger/etc. And you shoot an arrow by clicking on the screen and the archer will rotate his upper body, his arms with the bow and the head so that the bow is looking at the Point where you clicked on the screen.
Here is the problem: How can I rotate or transform the mesh of my character, that he/she is looking at the point because when I just rotate the element by script, the animator sets the Rotation to the default Rotation changes of the Animation.
Is there any way to handle this or do you know of another method?

How do I make my character's arms point towards the camera or cursor in Unity?

I'm making a fps game and I want my character's arms to rotate up and down depending where the cursor/camera is pointing at. The character already rotates when I turn left or right. Hopefully someone can answer my question. I'm new to Unity and I'm still learning how to code in C#.
For top and bottom rotation, create an animation for top aim, eye level aim and feet aim. send the value of your rotation to animator controller and animate according to it.

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

iPhone - detecting circle movements

I have an area where the user can draw using a finger. This area contains an object that I want the user to be able to rotate it clockwise or anti-clockwise. My idea is to offer the user the whole screen to control the object. I mean, if the user starts to describe clock wise finger movements in a circle pattern, the object will rotate on that direction. If the movements are anti-clockwise the object will rotate the other direction.
So, the idea is to detect if the finger is describing circle movements, clockwise or anti-clockwise and the amount of angle. This has to be real-time, I mean, as far as the user is rotating the finger object is rotating.
I have seen apps doing something like that, where the user draws a shape and boom, the app replaces the clunky shape drawn with a pretty one. In essence the app detected that a circle, a triangle, etc., was drawn and replace that gesture with a real pretty shape.
How do I do this kind of stuff? I am just interested in circle movements.
Can you guys point me the direction?
thanks.
Check out the "hough transform for circle detection". Here is a good blog post to start with: