Unity how to rotate object to cursor
Unity how to rotate object to cursor
Game Dev Beginner
How to make an object follow the mouse in Unity (in 2D)
In Unity by John French August 26, 2021 8 Comments
When working with 2D in Unity, you may sometimes want to make an object interact with the mouse’s position in some way.
For example, perhaps you want to place a sprite under the cursor?
Maybe you want an object to follow the mouse’s position?
Or, what if you want an object to look towards the mouse, turning to face it wherever it is on screen?
Just like moving or rotating an object towards an object’s Transform position, the position of the mouse on the screen can also be used in the game, by converting it into a world position.
In this article, you’ll learn how to get the mouse’s position in the scene and how you can use it to move and rotate other objects towards the cursor.
Here’s what you’ll find on this page:
Let’s get started.
How to get the mouse position in the world in 2D
Getting the mouse position on the screen is relatively straightforward.
This works by getting the Mouse Position property of the Input Class, which returns the pixel coordinates of the cursor on the screen as a Vector 3 value.
Like this:
While the Mouse Position is returned as a Vector 3, it only includes X and Y values (which are the pixel coordinates on the screen).
It’s provided as a Vector 3 for compatibility reasons, allowing you to use it with functions that require a Vector 3 value. So, while there is a Z value in the Mouse Position property, it’s always zero.
Once you have the mouse’s screen coordinates, it’s possible to convert them to a real-world position, using the Screen to World Point function.
Like this:
Screen to World Point is a camera function, so you’ll need to call it from a specific camera or from the Main Camera, which is simply the first camera in the scene with a “Main Camera” tag.
The Screen to World Point function will then return a Vector 3 value, which you can use like any other position in the scene.
How to set the depth of the mouse position
While the X and Y values of the Vector 3 that’s returned will correspond with the screen position of the mouse, the Z value will be, by default, the same as the camera’s Z position.
Which, because the camera’s location is behind its Near Clip Plane, is outside of the visible area.
In many cases, this may not be a problem.
For example, when working in 2D, if you store the result of the Screen to World Point function as a Vector 2 value, the Z value that’s returned is simply not used.
However, if you use the Vector 3 that the Screen to World Point function returns to move an object to the mouse’s position, without offsetting the z value, it will be out of view of the camera.
So, if you’re trying to move an object to the mouse’s position, and it keeps disappearing, that could be why.
For this reason, if you’re working with Vector 3 values, you may wish to manually set the Z value of the position to a positive value, such as the camera’s Near Clip Plane, to make sure that, if you place an object there, that it’s always in view:
For more information about converting the mouse position to a world position in 2D and 3D, see my full guide here:
Getting the mouse position using the new Input System
The Mouse Position value is a property of the Input Class, which is Unity’s legacy system for detecting mouse, keyboard and controller input.
This means that, if you’re using Unity’s new Input System, this method of getting the mouse’s coordinates won’t work.
Instead, you’ll need to use the Mouse class, using the Input System namespace, to read the position of the cursor.
Like this:
For more information on using Unity’s new Input System, try my complete getting started guide:
How to move an object to the mouse’s position in 2D
Once you have the mouse’s position in the world, using it to change the position of other objects can be very simple.
For example, I could use the mouse’s world position to place a sprite underneath the cursor.
Like this:
By setting the position of the Transform that the script is attached to, the object, in this case, a 2D sprite, tightly follows the position of the mouse.
This can be useful for placing sprites and objects under the cursor, such as a crosshair:
Setting the position of an object, such as a sprite, to the mouse’s world position places it under the cursor.
How to move an object to the mouse’s position, without lag
While placing an object under the cursor by getting the mouse’s position in the world does work, you might notice that there’s a lag between the position of the pointer in your operating system and the object on the screen.
Generally speaking, there’s no getting around this.
While higher frame rates can reduce the amount of lag you experience, the mouse is controlled directly by the operating system, meaning it updates independently of the game’s framerate.
Because of this, you’ll always notice some difference between the operating system’s cursor and the in-game object, which can be a problem if you’re using this method to create a software cursor.
To fix it, Unity offers a solution to replace the system’s hardware cursor with a custom texture instead, using the Set Cursor method of the Cursor class.
Like this:
While getting the mouse’s position in the scene can be useful for snapping objects to the cursor’s location, chances are, you’re more likely to use this method as a way to make objects respond to the cursor’s position in-game.
For example, moving an object towards the mouse.
How to make a 2D object move towards the mouse’s position
Once you’ve gotten the position of the mouse in the scene, it’s possible to make an object move towards it.
This works in the same way as it would with any other object, as the mouse position is now simply a position in a world that can be used as a target to move towards.
For example, using the Move Towards function.
Like this:
This will make the object that this script is attached to follow the mouse’s position on the screen at a constant speed (in this case 10 units per second) without overshooting.
The Move Towards function moves an object towards a target position at a consistent speed.
And, if you want to ease the movement of the object so that it follows the mouse smoothly, starting and stopping more slowly, you can do that by using Smooth Damp.
Smoothly move an object towards the mouse (using Smooth Damp)
Smooth Damp works like Move Towards except that it eases the object’s movement between its starting point and its target.
The object will start slowly, accelerate to an optional maximum speed, and slow down as it approaches the target position.
Which looks like this:
Smooth Damp can be used to create a more natural movement, where the object will start slow, come up to speed and slow again as it reaches its target.
Smooth Damp can be used to ease many different types of values. In this case, it’s a function of the Vector 2 class:
The Smooth Damp function takes a Smooth Time, which is, approximately, the amount of time that the movement should take and, optionally, a Max Move Speed, which is the maximum allowed speed of the object when completing the movement:
Lastly, the Smooth Damp function needs a local value to work from, in this case, a Vector 2, to be able to track the velocity of the object between frames:
In the function, you’ll reference the local Current Velocity with the ref keyword.
What is the ref keyword in Unity?
The ref keyword in Unity works in a similar way to the out keyword, which is used to return additional information from a function.
However, while the out keyword is one way, the ref keyword works both ways. It is both set and used by the function.
In this case, the Smooth Damp function sets and reads the Current Velocity value, which is how it can perform a smoothing action over time, even though a new function is called each frame.
Here’s how it looks all together:
How to follow an object at a distance in 2D
The Move Towards and Smooth Damp functions both take a parameter for their target position.
Which means that, if you want an object to follow a target in 2D, such as the mouse or another object, but from a set distance, all you need to do is offset the position of the target in the direction of the following object.
So how does that work?
First, you’ll need to know the direction between the two objects.
This is because the offset that you’ll need to add defines a point on the path between the target and the following object.
How to get the direction between two objects in 2D
Getting a direction vector between two objects in 2D works by subtracting the position of the origin object from the position of the target and then normalising the result.
Or, put simply, start with the target position on the left and subtract the position the direction should start from.
Like this:
Normalising the direction vector limits its length to 1 (making it a Unit Vector).
This can be useful for defining distance in a specific direction as a Unit Vector, multiplied by a distance value, will create a direction vector with an exact length.
In this example, getting a unit vector from the mouse position and multiplying it by a Minimum Distance value, such as 2 units, will create a 2 unit offset in the direction of the object that’s following the mouse.
Like this:
This means that the object will follow the mouse, but at a distance, never moving closer than the value of Min Distance to the mouse’s position and smoothly moving away if it does get too close.
Which looks like this:
Smooth Damp will smoothly ease the object to the mouse’s position while setting a minimum distance, makes the object follow without getting too close.
The only reason for working out the direction between the two objects in this example is to work out which way the offset should point.
However, there are other reasons why you might want to work out the direction of another object.
For example, to make one object look at another.
Make a 2D object look at the mouse position
While it seems like a simple task, rotating one object so that it looks at another in 2D can sometimes be tricky.
This is mainly because otherwise convenient functions, such as Look At, don’t always produce the results you want.
A lot of the time, when you’re rotating an object in 2D, all you really want to do is rotate an object around its forward Axis, the Z-Axis.
If the object is a flat sprite, any rotation around the X or Y-Axis could cause the object to become invisible because of its orientation towards the camera.
Because of this, the simplest way to rotate an object in 2D, is around its Z-Axis, using a single float value.
Like this:
Then, all you’d need to do to rotate the 2D object is pass in the angle of rotation.
Except, how do you get the angle between the object that’s rotating and the position it’s supposed to be looking at?
How to get the angle between two 2D objects in Unity
To get the angle between two objects, in this case between an object and the mouse position, you’ll need to first get a direction vector between them.
Just like in the earlier example, getting a direction works by subtracting the starting position from the target.
In this case, that means subtracting the position of the object that’s supposed to be looking at the mouse from the mouse’s position in the world.
Like this:
To work out the angle, next you’ll need to decide which way is the object’s forward direction.
For example, if you have a simple 2D arrow:
Then the forward direction of that object is to the right (if you want the arrow to point at the mouse correctly that is).
Meaning that the angle you’d need to find is the angle between Vector2.right and the direction of the mouse from the object.
So how can you do that?
Helpfully, the Signed Angle function in Unity does exactly that.
Then, to rotate the object, all you need to do is set the object’s Z rotation to the angle value:
Which looks like this:
Calculating the angle between the object and the mouse makes it easier to rotate in 2D, by only using the Z-Axis.
Angle vs Signed Angle in Unity
There are two different functions in Unity for getting an angle between two vectors, Angle and Signed Angle.
While they both work in similar ways, returning the smallest of the two possible angles between the vectors, the type of value they return is slightly different.
For example, Angle returns an unsigned value, meaning it can only be positive. In this case, that means that the angle will always be between 0 and 180 degrees.
For this use case, that means that, when the angle between the two vectors is less than 0 degrees, or more than 180, the object starts to rotate away from the mouse.
Using the Angle function, instead of Signed Angle, causes the object to rotate away from the mouse at negative angles.
Here it is all together:
How to rotate a 2D object smoothly
In the same way that it’s possible to smooth the movement of an object towards the mouse, the rotation of an object can also be eased.
Typically, there are two ways of doing this.
Either with the Rotate Towards function, which will rotate the object at a consistent max speed (measured in degrees per second)
Like this:
Which looks like this:
Rotate Towards will rotate the object to face the mouse at a consistent speed, measured in degrees per second.
Or, the second option is to smooth the rotation using Smooth Damp.
Using Smooth Damp will cause the object to turn to look at the mouse in a smoother, more natural motion that is eased at the start and end of its movement.
Like this:
Notice that this method uses a specific type of Smooth Damp, Smooth Damp Angle, which is a Maths function that allows the angle value to move past 180 degrees.
Instead, the arrow freely rotates to look at the mouse, no matter where it is.
Which looks like this:
Smooth Damp Angle eases the rotation of the object towards its target.
Make a 2D projectile follow the mouse in Unity
The separate methods of moving an object and looking towards the mouse can be combined to make an object follow the mouse position.
For example, to create a missile projectile, that moves towards the mouse, you could combine the method of rotating an object towards the mouse with a simple script that moves the object forwards at a set speed.
Like this, for example:
The result is a projectile that moves forwards while turning towards the mouse position.
Which looks like this:
Combining the Look At script with a script that moves the object forward creates a homing missile.
The accuracy of the projectile depends on how quickly it can turn, where a higher turn speed, in degrees per second, means that the projectile can face the target position sooner, making a tighter turn towards it.
Like this:
Increasing the turn speed makes the projectile turn faster and follow more tightly.
But what about physics objects?
If your projectile has a Collider attached to it, it’s usually a bad idea to move it around using its Transform.
This is to do with the performance hit of moving a Static Collider, which is simply any object with only a Collider attached, using its Transform component.
Typically, in 2D, you’ll get better performance when moving it with a Rigidbody component, in this case, set to a Kinematic Body Type.
Helpfully, you’ll still be able to use the same calculations to manage angle and movement as when moving the object using its Transform.
However, instead of setting the position of the object directly, you’ll need to set the position and rotation of the Rigidbody using the Move Position and Move Rotation functions.
Like this:
Now it’s your turn
Now I want to hear from you…
How are you using the mouse position with other objects in your game?
Are you turning or moving objects towards the mouse?
And what have you learned about working with the mouse position in 2D that you know others will find useful?
Whatever it is let me know by leaving a comment.
Image attribution
by John Leonard French
Game audio professional and a keen amateur developer.
Get Game Development Tips, Straight to Your inbox
Get helpful tips & tricks and master game development basics the easy way, with deep-dive tutorials and guides.
My favourite time-saving Unity assets
Rewired (the best input management system)
Rewired is an input management asset that extends Unity’s default input system, the Input Manager, adding much needed improvements and support for modern devices. Put simply, it’s much more advanced than the default Input Manager and more reliable than Unity’s new Input System. When I tested both systems, I found Rewired to be surprisingly easy to use and fully featured, so I can understand why everyone loves it.
DOTween Pro (should be built into Unity)
An asset so useful, it should already be built into Unity. Except it’s not. DOTween Pro is an animation and timing tool that allows you to animate anything in Unity. You can move, fade, scale, rotate without writing Coroutines or Lerp functions.
Easy Save (there’s no reason not to use it)
Easy Save makes managing game saves and file serialization extremely easy in Unity. So much so that, for the time it would take to build a save system, vs the cost of buying Easy Save, I don’t recommend making your own save system since Easy Save already exists.
Comments
Hi John, I had some specific questions for you regarding moving objects with the mouse. I’m open to paying for some scripting. I’ll give my email address associated with this comment so you can contact me if interested.
Hi Walter, I’m already working on an article about that so I may already be covering what you need. If you could let me know what you’re after I can work it into the article. All the best, John.
Hey John, I’ve been looking for many tutorial about how to make my game object follow mouse position in order to complete my assignment. And then I found your tutorial, THIS tutorial, you’re really a kind man giving this easy to learn tutorial. I like this one, Thanks John.
You’re welcome, glad it helped!
Any idea on how you could make the object follow at a distance, but only on the x axis? thanks in advance!
The examples in this article directly affect the object’s transform position, but what you could do is use the same code to set a Vector 2 variable and then lock one part of that to whatever value you want it to be, after you’ve calculated the position. Something like:
Vector2 newPosition = // Code that would have set the transform goes here;
transform.Position = new Vector2( newPosition.x, float lockedValue);
This is off the top of my head but something like that should work.
Thanks a lot again!. I’ve just discovered this web and it’s a hidden gem!!
My God! what a tutorials, incredibly detailed… Wow! is the only thing I can say.
I’ve went through some of the tutorials and all of them have the same level of detail and love.
Thanks a lot for your effort making this tutos.
How to rotate an object around a fixed point so it follows the mouse cursor?
I want to rotate the purple rectangle around the red square in reference to the center of the square. In addition I want to be able to have the rectangle follow the mouse cursor wherever it goes. Here is a picture for demonstration:
Here is the code I have already written:
This code doesn’t work and just rotates the rectangle around the square at a fixed pace with no noticeable influence from the mouse. Thank you for your help.
Here to be more clear. This takes place in unity3d but appears as a 2d game. Y is the direction facing the camera. I want the rectangle to only rotate around the center of the red Square(really a cube) only around the y axis(the rectangle is actually above the cube, so dont worry about them colliding). Both objects will move around with the movement keys at the same time.
1 Ответ
Ответ от robertbu · 20/09/14 07:30
Here is one solution. If the center object does not move, you can move the calculation of ‘centerScreenPos’ to Start().
Sorry this doesn’t work for me. The rectangle it seems moves around the x axis and I want to only rotate it along the y axis relative to a point. I made another comment to my original post that hopefully will clarify my intent better.
For the camera looking down the ‘y’ axis, change line 20 to:
Rotate OBJECT to face mouse cursor for x/z based top down shooter
Normally I’d avoid reposting a repost but I’ve tried every single solution proffered in the other threads and they do not work. One reason, among several, is that they all use the camera to calculate the rotation angle in relation to the mouse and then rotate the camera to it. This is not desirable.
Instead, I want the primary game object to rotate to face the mouse cursor while leaving the camera in its original orientation.
Few caveats: I’m not a coder. I’m using uScript for most of my development. I’ll also mention that this has kept me scratching my head for almost a week, so any prescriptive solutions that can get me over this hump are really appreciated.
3 Ответов
Ответ от Mox.du · 12/11/11 05:01
I am not sure what is the problem exactly but here are two codes for rotating object toward mouse cursor around its Y axis (I believe that this is what you wanted).
Code 1: Here you have speed while rotating towards mouse. You can get rid of speed if you uncomment last line of code and comment previous.
// LookAtMouse will cause an object to rotate toward the cursor, along the y axis. // // To use, drop on an object that should always look toward the mouse cursor. // Change the speed value to alter how quickly the object rotates toward the mouse.
// speed is the rate at which the object will rotate var speed = 5;
var mouse_pos : Vector3; var target : Transform; //Assign to the object you want to rotate var object_pos : Vector3; var angle : float;
Both codes works. Something you should always do when taking code snippets like this is to make a test with it. Open new scene, add object attach code and test, because none of these codes was written for your specific scene in sense of object na$$anonymous$$g etc. It is also good to read comments in scripts which will give you some clues.
I tried the first code and it works fine except that as my object rotates to face the mouse, the mouse is also being moved, so it continuously spirals around.
Thanks! This helped me alot.
Ответ от Mox.du · 13/11/11 00:10
Scripts are working with transforms only, so if your model CVs/Vertices are being altered I don’t think that script is the cause by itself. Perhaps you have some children problems or bone problems or something like that. If you don’t solve this you can upload small package with just the object and script, showing problem and I will take a look.
Ответ от Robo885 · 14/09/12 00:10
I have tried the first block of code, applying it two either my player, or the spotlight I have attached to it. My goal is to have the light face the direction of the mouse, while the camera stays put and the player can move with WASD.
However, I keep getting NullReferenceExpection. Being new, I have tried to solve this problem, but have run in to no good luck. Help would be much appreciated!
NullReferenceException UnityEngine.Camera.ScreenPointToRay (Vector3 position) Mouse_Tracking.Update () (at Assets/Mouse_Tracking.js:14)
The above implies that line 14 is the issue;
`var ray = Camera.main.ScreenPointToRay (Input.mousePosition);`
Hopefully this is enough information for a kindly stranger to help?
Positioning GameObjects
To select a GameObject, click on it in the Scene view or click its name in the Hierarchy window. To select or de-select multiple GameObjects, hold the Shift key while clicking, or drag a rectangle around multiple GameObjects to select them.
Selected GameObjects are highlighted in the Scene view. By default, this highlight is an orange outline around the GameObject; to change the highlight color and style, go to Unity > Preferences > Color and edit the Selected Wireframe and Selected Outline colours. See documentation on the Gizmo Menu for more information about the outline and wireframe selection visualizations. The selected GameObjects also display a Gizmo in the Scene view if you have one of the four Transform tools selected:
Move, Rotate, Scale, and RectTransform
The first tool in the toolbar, the Hand Tool, is for panning around the Scene. The Move, Rotate, Scale, Rect Transform and Transform tools allow you to edit individual GameObjects. To alter the Transform component of the GameObject, use the mouse to manipulate any Gizmo axis, or type values directly into the number fields of the Transform component in the Inspector.
Alternatively, you can select each of the four Transform modes with a hotkey: W for Move, E for Rotate, R for Scale, T for RectTransform, and Y for Transform.
The Move, Scale, Rotate, Rect Transform and Transform Gizmos
At the center of the Move Gizmo, there are three small squares you can use to drag the GameObject within a single plane (meaning you can move two axes at once while the third keeps still).
If you hold shift while clicking and dragging in the center of the Move Gizmo, the center of the Gizmo changes to a flat square. The flat square indicates that you can move the GameObject around on a plane relative to the direction the Scene view Camera is facing.
Rotate
With the Rotate tool selected, change the GameObject’s rotation by clicking and dragging the axes of the wireframe sphere Gizmo that appears around it. As with the Move Gizmo, the last axis you changed will be colored yellow. Think of the red, green and blue circles as performing rotation around the red, green and blue axes that appear in the Move mode (red is the x-axis, green in the y-axis, and blue is the z-axis). Finally, use the outermost circle to rotate the GameObject around the Scene view z-axis. Think of this as rotating in screen space.
Scale
The Scale tool lets you rescale the GameObject evenly on all axes at once by clicking and dragging on the cube at the center of the Gizmo. You can also scale the axes individually, but you should take care if you do this when there are child GameObjects, because the effect can look quite strange.
RectTransform
The RectTransform is commonly used for positioning 2D elements such as Sprites or UI elements, but it can also be useful for manipulating 3D GameObjects. It combines moving, scaling and rotation into a single Gizmo:
Note that in 2D mode, you can’t change the z-axis in the Scene using the Gizmos. However, it is useful for certain scripting techniques to use the z-axis for other purposes, so you can still set the z-axis using the Transform component in the Inspector.
For more information on transforming GameObjects, see documentation on the Transform Component.
Transform
The Transform tool combines the Move, Rotate and Scale tools. Its Gizmo provides handles for movement and rotation. When the Tool Handle Rotation is set to Local (see below), the Transform tool also provides handles for scaling the selected GameObject.
Gizmo handle position toggles
The Gizmo handle position toggles are used to define the location of any Transform tool Gizmo, and the handles use to manipulate the Gizmo itself.
For position
Click the Pivot/Center button on the left to toggle between Pivot and Center.
For rotation
Click the Local/Global button on the right to toggle between Local and Global.
Unit snapping
While dragging any Gizmo Axis using the Move tool or the Transform tool, hold the Control key (Command on Mac) to snap to increments defined in the Snap Settings (menu: Edit > Snap Settings…)
Surface snapping
While dragging in the center using the Move tool or the Transform tool, hold Shift and Control (Command on Mac) to quickly snap the GameObject to the intersection of any Collider.
Look-at rotation
While using the Rotate tool or the Transform tool, hold Shift and Control (Command on Mac) to rotate the GameObject towards a point on the surface of any Collider.
Vertex snapping
Use vertex snapping to quickly assemble your Scenes: take any vertex from a given Mesh and place that vertex in the same position as any vertex from any other Mesh you choose. For example, use vertex snapping to align road sections precisely in a racing game, or to position power-up items at the vertices of a Mesh.
Follow the steps below to use vertex snapping:
Select the Mesh you want to manipulate and make sure the Move tool or the Transform tool is active.
Press and hold the V key to activate the vertex snapping mode.
Move your cursor over the vertex on your Mesh that you want to use as the pivot point.
Hold down the left mouse button once your cursor is over the vertex you want and drag your Mesh next to any other vertex on another Mesh.
Release the mouse button and the V key when you are happy with the results (Shift+V acts as a toggle of this functionality).
Note: You can snap vertex to vertex, vertex to surface, and pivot to vertex.
Screen Space Transform
While using the Transform tool, hold down the Shift key to enable Screen Space mode. This mode allows you to move, rotate and scale GameObjects as they appear on the screen, rather than in the Scene.
Transform tool added in 2017.3 NewIn20173
2017–05–18 Page amended with editorial review
Did you find this page useful? Please give it a rating:
How to rotate Object with Mouse drag (specifications below)
First of all, I KNOW that this question is already been answered. But what I want to do exactly is rotate an object with mouse…
From the Top view (rotation along Y axis only)
3D, not 2D (I’ll use a basic Perspective camera, not Orthographic)
Including a “speed” variable that I can have rotation control with it (this one just if it’s possible).
**I’ve been trying to adapt other scripts, but I just can’t make it correct. These are four specifications I couldn’t solve and I need together.
2 Ответов
Ответ от bloeys · 22/01/16 04:22
Hey! sorry for the delay, was a bit busy.
So what you actually wanted was a ‘look at mouse script’, not simply a way to rotate on an axis with speed. Well the solution might look a but complicated but its actually simple, I hope you will get it.
So only one thing to note here is that you probably should let the rotation speed be a bit high, because if its a bit low and the mouse rotates too quickly around the object the object will suddenly switch directions, which doesn’t look natural, anyway it isn’t that much of a problem.
Hope this is what you want 😀
O$$anonymous$$G thanks a lot. This is more than EXACTLY what I needed
never thought I would get it, I recognize it
Now trying to averiguate how to mark it as correct answer.
Not at all 😉 I have converted to an answer so you can properly mark it. All the best with your projects 🙂
Hello, what would be the proper way to change this so that it rotates along the Z axis as well?
Ответ от bloeys · 13/01/16 18:54
You can then combine these with something like transform.Rotate to make your object rotate on its x&y axes, and I recommend making the rotation relative to world space so its consistent. If you want rotation on the z axis as well then I guess you will need to use another input(There might be a way to do it with the mouse though) like two extra buttons and combine their input with the mouse input.
Thank you it helps! but I’m not going to solve my problem because I’m not very good at program$$anonymous$$g. I was trying to use this script for now:
The problem here is that it just works with an Orthographic camera setup because is 2D, If there were just a few lines to change in order to rotate from Prespective camera…
I’m almost ending an entire game ready to publish and I need this so much, so thanks if someone knows it.
I see, no problem then I’ll put some code and I’ll try to guide you through it the best I can. Now you said that you only want rotation on the y axis, so the code will only take care of that.
Now by attaching this script to an object, that object will continuously rotate with the mouse, but only on the y axis. Of course you can put some simple checks to make it only rotate when you want it to.
Feel free to ask anything 🙂
Thank you so much, this is it!! I didn’t expect that approximate answer, I appreciate it a lot!
But there is just one thing missing, well the most important actually; this rotation is along X axis, and it should rotate along Y axis.
To do so, I’ve changed “cam.right” to “cam.forward”, but you will notice about the problem here… It basically rotates correct on half screen and negative to mouse position at other half screen.
There is just missing something I guess. Thanks again!
Источники информации:
- http://answers.unity.com/questions/794119/how-to-rotate-an-object-around-a-fixed-point-so-it.html
- http://answers.unity.com/questions/185346/rotate-object-to-face-mouse-cursor-for-xz-based-to.html
- http://docs.unity3d.com/2017.3/Documentation/Manual/PositioningGameObjects.html
- http://answers.unity.com/questions/1125850/how-to-rotate-object-with-mouse-drag-specification.html