## When I make an object face a mouse cursor in Unity it eventually offsets - c#

I made a script that makes the player point towards the mouse cursor, but recently I discovered a bug. When I move the mouse cursor too much (An example being when I spin the mouse around the object in circles, causing the object to move around.), the object ends up pointing a bit off of where the mouse should be. As in, the cursor would signal the object to look at it, and the object ends up looking the slightest bit off, making it feel quite odd after maneuvering quickly. How can I make it so the object always faces the cursor, with no offsets, even when I move the cursor as much as possible.
private void LateUpdate()
{
Vector3 lookAtPoint = Camera.main.ScreenToWorldPoint(Input.mousePosition);
Vector3 mousePoint = new Vector3(lookAtPoint.x, lookAtPoint.y, 0);
float angle = getAngle(transform.position, mousePoint);
transform.rotation = Quaternion.Lerp(transform.rotation, Quaternion.Euler(0, 0, angle), 9f * Time.deltaTime);
float getAngle(Vector3 currentLocation, Vector3 mouseLocation)
{
float x = mouseLocation.x - currentLocation.x;
float y = mouseLocation.y - currentLocation.y;
return angle = Mathf.Rad2Deg * Mathf.Atan2(y, x);
}
}

Looks like it's down to the way that you are using Quaternion.Lerp(). In particular, the last parameter - it is meant to be a value between 0f and 1f which you supply, it does not auto-increment for you.
So to fix this issue, what you want to do is save off a float somewhere. When you move the mouse (mouse position has changed since last frame) then start that value at 0f again. Then increment it at some value every frame until it is equal to or greater than 1f.
Doing this will not only fix your bug. It will, depending on how fast your increment, give you a smooth rotation effect. Below is an example.
internal class SomeClass : MonoBehaviour
{
private float lerpAmount = 0f;
private Vector3 cachedMousePosition = Vector3.zero;
private void LateUpdate()
{
var mousePosition
= Camera.main.ScreenToWorldPoint(Input.mousePosition)
* new Vector3(1, 1, 0);
bool recalculateRotation = false;
if (this.cachedMousePosition != mousePosition)
{
this.lerpAmount = 0f;
recalculateRotation = true;
}
if (this.lerpAmount < 1f)
{
this.lerpAmount = Mathf.Min(this.lerpAmount + Time.deltaTime, 1f);
recalculateRotation = true;
}
if (recalculateRotation)
{
float angle = getAngle(transform.position, mousePoint);
transform.rotation = Quaternion.Lerp(transform.rotation, Quaternion.Euler(0, 0, angle), this.lerpAmount);
}
float getAngle(Vector3 currentLocation, Vector3 mouseLocation)
{
float x = mouseLocation.x - currentLocation.x;
float y = mouseLocation.y - currentLocation.y;
return angle = Mathf.Rad2Deg * Mathf.Atan2(y, x);
}
}

## Related

### How to use LookRotation to rotate an object based on hand positions?

I'm creating an interaction system for a VR game and I'm struggling with two hand interactions. I'm using the Quaternion.LookRotation() method to generate the rotation of the grabbed object based on the positions and rotations of the hands. The forward part is pretty simple: Vector3 fwd = (primaryHand.position - secondaryHand.position).normalized; The "up" part is what I have difficulty with. Initially I tried using the average up direction of both hands: Vector3 avgUp = Quaternion.Slerp(primaryHand.rotation, secondaryHand.rotation, 0.5f) * Vector3.up; There is an issue with this approach: the hand's up vector might get aligned with the fwd vector, which causes the object to flip over when it goes over it. Here is a simple illustration of the problem: The light green arrows represent the up direction of the hands, while the dark green is the calculated direction used as an argument for the LookRotation() method. The obvious solution seems to be to pick a different fixed vector instead of "up", one which won't be so easily aligned with the fwd vector. In the example it could be a vector aligned with the fingers. But keep in mind that there are no restrictions on initial hand rotation so no matter which vector you choose the hands can always happen to be rotated so that the vectors align. And even if you pick the an optimal vector dynamically (one that is perpendicular to fwd), it's still at best 90 degrees from aligning with fwd. To solve this I tried restricting the direction to the values which don't cause problems but this caused another issues (I had difficulties with determining which values are ok and which should be discarded). I feel like I'm doing something wrong here, is there any better solution to this problem?

You can calculate the delta rotations of the hands and apply it to the "base up" of the object (the new up if we only take into account the change in position of hands...which will of course be orthogonal to the axis of the object). Then determine the change in angle that results in that up being rotated with each hand. Average those angles out, then apply those angles with the hand-hand axis using Quaternion.AngleAxis to the "base up" from earlier. Then you have your forward and up for Quaternion.LookRotation). Below is an example of how you can use this, including VR hand noise simulation. To see the test, create a new scene in unity and attach this to the camera and it will build the scene on play start. There is a grip/release gui that will appear in Play view. You can adjust the hand rotation in Scene view Vector3 leftHandPosCurrent; Vector3 rightHandPosCurrent; Vector3 worldAxisPrev; Quaternion leftHandRotPrev; Quaternion leftHandRotCurrent; Quaternion rightHandRotPrev; Quaternion rightHandRotCurrent; bool isGripping; bool firstFrameGripping; Rigidbody grippedRB; Transform leftHand; Transform rightHand; Quaternion targetRot; /* * On subsequent frames of gripping, calculate deltas in positions and * rotations, average out the hand's effects, then apply them to the gripped * object */ void HandleGrippedRot() { Vector3 worldAxisCurrent = rightHandPosCurrent - leftHandPosCurrent; if (!firstFrameGripping) { Vector3 prevUp = targetRot * Vector3.up; // we haven't moved the transform based on the hands yet, so // find the new up would be ignoring hand rotations Vector3 newUp = Quaternion.FromToRotation(worldAxisPrev, worldAxisCurrent) * prevUp; float leftHandAngle = GetDegRot(newUp, leftHandRotPrev, leftHandRotCurrent, worldAxisCurrent); float rightHandAngle = GetDegRot(newUp, rightHandRotPrev, rightHandRotCurrent, worldAxisCurrent); float avgAngle = (leftHandAngle + rightHandAngle) * 0.5f; newUp = Quaternion.AngleAxis(avgAngle, worldAxisCurrent) * prevUp; targetRot = Quaternion.LookRotation(worldAxisCurrent, newUp); } else { firstFrameGripping = false; } leftHandRotPrev = leftHandRotCurrent; rightHandRotPrev = rightHandRotCurrent; worldAxisPrev = worldAxisCurrent; } /* * Given the "up" of the object without taking hand rotations into account * and the axis, determine how a hand's delta rotation affects that up * around the axis and return the angle of that rotation */ float GetDegRot(Vector3 baseUp, Quaternion prevHandRot, Quaternion curHandRot, Vector3 axis) { Vector3 adjUp = (curHandRot * Quaternion.Inverse(prevHandRot)) * baseUp; adjUp = Vector3.ProjectOnPlane(adjUp, axis); return Vector3.SignedAngle(baseUp, adjUp, axis); } void Update() { AddVRNoise(leftHand); AddVRNoise(rightHand); leftHandPosCurrent = leftHand.position; rightHandPosCurrent = rightHand.position; leftHandRotCurrent = leftHand.rotation; rightHandRotCurrent = rightHand.rotation; if (isGripping) { HandleGrippedRot(); } } void StartGrip() { if (isGripping) return; isGripping = true; firstFrameGripping = true; // grippedTransform is set accordingly at some point } void EndGrip() { if (!isGripping) return; isGripping = false; } /* * example of using targetRot to move rb */ private void FixedUpdate() { if (!isGripping) return; Quaternion delta = targetRot * Quaternion.Inverse(grippedRB.transform.rotation); delta.ToAngleAxis(out float angle, out Vector3 axis); // convert to shortest angle form if (angle > 180f) { axis = -axis; angle = 360f - angle; } grippedRB.angularVelocity = angle * 0.25f * axis; } /* * just for testing purposes */ void Start() { leftHand = CreateHand(true); leftHand.position = Vector3.left; rightHand = CreateHand(false); rightHand.position = Vector3.right; CreateArrow(); } /* * just for testing purposes */ void AddVRNoise(Transform hand) { Quaternion noise = Random.rotation; noise.ToAngleAxis(out float angle, out Vector3 axis); angle = 100f * Time.deltaTime; noise = Quaternion.AngleAxis(angle, axis); Quaternion noisyRot = hand.rotation * noise; hand.rotation = noisyRot; } /* * just for testing purposes */ void OnGUI() { if (GUI.Button(new Rect(0, 0, 100, 50), "Grip")) { StartGrip(); } if (GUI.Button(new Rect(100, 0, 100, 50), "Release")) { EndGrip(); } } /* * just for testing purposes */ Transform CreateHand(bool isLeft) { string handName = isLeft ? "Left" : "Right"; GameObject hand = new GameObject($"{handName}hand"); GameObject palm = GameObject.CreatePrimitive(PrimitiveType.Cube); palm.transform.localScale = new Vector3(0.5f, 0.2f, 1f); palm.transform.SetParent(hand.transform); GameObject thumb = GameObject.CreatePrimitive(PrimitiveType.Cube); thumb.transform.localScale = new Vector3(0.2f, 0.1f, 0.1f); thumb.transform.SetParent(hand.transform); thumb.transform.localPosition = new Vector3(isLeft ? 0.32f : -0.32f, 0f, -.31f); return hand.transform; } /* * just for testing purposes */ void CreateArrow() { GameObject arrow = new GameObject(); GameObject body = GameObject.CreatePrimitive(PrimitiveType.Cube); body.transform.localScale = new Vector3(1f, 1f, 5f); body.transform.SetParent(arrow.transform); GameObject head = GameObject.CreatePrimitive(PrimitiveType.Cube); head.transform.SetParent(arrow.transform); head.transform.localEulerAngles = Vector3.up * 45f; head.transform.localPosition = new Vector3(0f, 0f, 2.5f); grippedRB = arrow.AddComponent<Rigidbody>(); grippedRB.useGravity = false; arrow.transform.position = 2f * Vector3.up; }

### Cylinder Snapping into place during Quaternion.RotateTowards

What I am trying to do is have a pillar in the center of a scene, and what i think is happening is the quaternion.RotateTowards is receiving a different starting/initial quaternion than the action which is causing it to snap/teleport into a different location which then starts to move to the next. I thought it may be because i'm misunderstanding how quaternions are handled in unity, so i've tried messing with normalizing it, but i can't seem to get any change on the teleport. The goal is to attach this scrip to a simple 3D cylinder and have it wobble basically, where there will be a player on top of it trying to stay on it. However I can't seem to figure out why it is teleporting and was hoping for a second set of eyes. using System.Collections; using System.Collections.Generic; using UnityEngine; public class PlatformWobble : MonoBehaviour { public float timeDelay = 0; public float rRange = 0.5f; public float maxRotation = 20.0f; public float rotRate = 0.05f; private bool wobble = false; private Vector3 randomRotation; private Quaternion rotation; private Quaternion destination; private float x, y, z; private bool inPlace; private void Start() { randomRotation = new Vector3(); inPlace = true; } // Update is called once per frame void FixedUpdate() { if (inPlace) { destination = RotateRandomly(); inPlace = false; } transform.localRotation = Quaternion.RotateTowards(transform.localRotation, destination, rotRate * Time.deltaTime); if (transform.localRotation == destination) { inPlace = true; } } //This will grab the rotation of the object and move it randomly, but won't exceed maxRotation; Quaternion RotateRandomly() { randomRotation = new Vector3(Random.Range(-rRange, rRange), Random.Range(-rRange, rRange), Random.Range(-rRange, rRange)); rotation = transform.localRotation; x = rotation.x + randomRotation.x; if(x >= maxRotation && x <= 360 - maxRotation) { x = rotation.x; } y = rotation.y + randomRotation.y; if (y >= maxRotation && y <= 360 - maxRotation) { y = rotation.y; } z = rotation.z + randomRotation.z; if (z >= maxRotation && z <= 360-maxRotation) { z = rotation.z; } return new Quaternion(x, y, z, transform.localRotation.w); } }

Your quaternion calculations are incorrect: quaternions do not hold angles, instead, they hold the representation of a rotation about an axis. I emphasized "representation" because it is a little complicated... The x, y, z components of a quaternion hold the rotation axis unit vector scaled by the sine of the half angle of rotation. The w component holds the cosine of the half angle. That is... // NOTE: rotationAngle is in radians rather than degrees Quaternion MakeQuaternion (Vector3 rotationAxis, float rotationAngle) { float c = Mathf.Cos (rotationAngle / 2); float s = Mathf.Sin (rotationAngle / 2); Vector3 v = rotationAxis.normalized * s; return new Quaternion (v.x, v.y, v.z, c); } Now, the tricky part for your problem is coming up with the rotation axis and angle for the desired effect. One solution (if you wish to stick to Euler angles) is to compute quaternions for each Euler rotation and then combine them: Quaternion EulerToQuat (float XAngle, float YAngle, float ZAngle) { Quaternion X = MakeQuaternion (Vector3.right, XAngle); Quaternion Y = MakeQuaternion (Vector3.up, YAngle); Quaternion Z = MakeQuaternion (Vector3.forward, ZAngle); // combine the rotations such that the object is first rotated about the Z axis, // then about the Y axis, then the X (ie, reverse order of multiplication). // Reminder: quaternion multiplicate is not commutative: order matters, so if this // is not the order you want, just siwtch things around rotate X * Y * Z; }

FixedUpdate doesn't get called every render frame, only on physics frames. You need a way to tell Unity to have each frame showing the rotation change, rather than only updating the rendering when the physics frame has just run. That is what Rigidbody.MoveRotation is for. Cache a reference to the Rigidbody, calculate the new global rotation it should have, then call MoveRotation: private Rigidbody rb; private void Start() { randomRotation = new Vector3(); inPlace = true; rb = GetComponent<Rigidbody>(); } // ... // Update is called once per frame void FixedUpdate() { if (inPlace) { destination = RotateRandomly(); inPlace = false; } Quaternion newLocalRot = Quaternion.RotateTowards(transform.localRotation, destination, rotRate * Time.deltaTime); Quaternion newGlobalRot = transform.parent.rotation * newLocalRot; rb.MoveRotation(newGlobalRot); if (transform.localRotation == destination) { inPlace = true; } }

### How to detect if Mouse stopped moving in Unity

I want to do a Game like slither.io in Unity but it's hard to detect when the mouse stopped moving, I want the ball to not stop, instead it should move on, worked several hours on this problem but can't quite get it to work. I thought if I save where the last known position is I can keep the velocity but I don't know how to implement yet. Thanks in advance! This is my Source Code: private float xMin, xMax, yMin, yMax; [SerializeField] float constantSpeed = 100f; [SerializeField] float padding = 5f; private Rigidbody2D rb2D; private Vector3 mousePosition; private Vector2 direction; private List<Vector3> ListPos = new List<Vector3>(); private Vector3 empty; // Use this for initialization void Start () { empty = Vector3.zero; SetUpMoveBoundaries(); rb2D = gameObject.GetComponent<Rigidbody2D>(); } // Update is called once per frame void Update () { Move(); } private void SetUpMoveBoundaries() { Camera gameCamera = Camera.main; xMin = gameCamera.ViewportToWorldPoint(new Vector3(0, 0, 0)).x + padding; xMax = gameCamera.ViewportToWorldPoint(new Vector3(1, 0, 0)).x - padding; yMin = gameCamera.ViewportToWorldPoint(new Vector3(0, 0, 0)).y + padding; yMax = gameCamera.ViewportToWorldPoint(new Vector3(0, 1, 0)).y - padding; } private void Move() { if (Input.mousePosition != empty) { mousePosition = Camera.main.ScreenToWorldPoint(Input.mousePosition); direction = (mousePosition - transform.position).normalized; ListPos.Add(direction); rb2D.velocity = new Vector2(direction.x * constantSpeed, direction.y * constantSpeed); } else { var last = ListPos.LastOrDefault(); rb2D.velocity = new Vector2(last.x * constantSpeed, last.y * constantSpeed); } }

Assuming you want it to trigger one of the two functions depending on whether the mouse has moved or not since last frame: Vector2 lastMousePosition; void WhenMouseIsMoving() { } void WhenMouseIsntMoving() { } void Update() { if (Input.mousePosition!=lastMousePosition) { lastMousePosition=Input.MousePosition; WhenMouseIsMoving(); } else WhenMouseIsntMoving(); } You'll need to add one more bool variable to keep track of whether it has just started or stopped moving;

If you want to check for mouse movement without having to keep track of the mouse position in a variable, you can use the GetAxis function from Unity's Input class. To do this you will have to make sure that mouse movement is hooked up to axes in Unity's input manager. It is normally there by default already, with 'Mouse X' and 'Mouse Y' mapped to the mouse delta for their respective axes. For example: // Is true when the mouse has moved if (Input.GetAxis("Mouse X") != 0 || Input.GetAxis("Mouse Y") != 0) { // Do something with mouse input } In your case, it looks like you could do something like this as your move function: Vector2 direction = new Vector2(Input.GetAxis("Mouse X"), Input.GetAxis("Mouse Y")); if (direction.magnitude != 0) { // ListPos.Add(direction); rb2D.velocity = direction.normalized * constantSpeed; } As an unrelated note, your ListPos variable is growing in size potentially every frame without limit. If you need to keep track of previous positions for reasons other than your attempt at detecting mouse position changes, you should consider how much storage you will need and give it a fixed size replacing the oldest entries, or consider whether or not the values need to be independent or can be merged instead.

### How to stop rotation by LerpAngle after -90°

I have colliders where i can turn to the sides and i want camera to rotate as a player turn to the side. I'm using Mathf.LerpAngle for that, but when I press the key for turn to side, camera is rotating in loop. How can I make rotation stop? The problem is that everytime I turn player should go -90 degrees to the left +90 to the right and there will be more turns so i can't use functions for setting rotation. I was already trying to make it stop by that if statement with (lAngle > 90f) float lAngle = Mathf.LerpAngle(minAngle, lMaxAngle, Time.deltaTime); float rAngle = Mathf.LerpAngle(minAngle, rMaxAngle, Time.deltaTime); Quaternion leftRotation = Quaternion.Euler(new Vector3 (0, lAngle, 0)); Quaternion rightRotation = Quaternion.Euler(new Vector3 (0, rAngle, 0)); transform.position = player.transform.position + offSet; transform.LookAt (player.transform); if (Input.GetKeyDown (KeyCode.LeftArrow) && GameObject.Find("Player").GetComponent<PlayerMovement>().turn) { turnLeft = true; } else if (Input.GetKeyDown (KeyCode.RightArrow) && GameObject.Find("Player").GetComponent<PlayerMovement>().turn) { turnRight = true; } if(turnLeft) { offSet = leftRotation * offSet; transform.position = player.transform.position + offSet; transform.LookAt (player.transform); if (lAngle > 90f) turnLeft = false; } if(turnRight) { offSet = rightRotation * offSet; transform.position = player.transform.position + offSet; transform.LookAt (player.transform); if (rAngle < -90f) turnRight = false; }

An alternative would be to use Quaternion.RotateTowards, as this allows rotations to negative values without the GameObject infinitely rotating (as it can't approach a negative value). Below you can see I'm storing the initial value of the GameObject's rotation, as a Vector3, before modifying this value whenever A or D keys are pressed. The rotation of the transform is then set to the result of Quaternion.RotateTowards. public class FixedRotate : MonoBehaviour { [SerializeField] private float m_rotationAngle; [SerializeField] private float m_rotationSpeed; private Vector3 m_targetRotation; public void Start() { m_targetRotation = transform.eulerAngles; } public void Update() { //Left if(Input.GetKeyDown(KeyCode.A)) m_targetRotation.y -= m_rotationAngle; //Right if (Input.GetKeyDown(KeyCode.D)) m_targetRotation.y += m_rotationAngle; transform.rotation = Quaternion.RotateTowards(transform.rotation, Quaternion.Euler(m_targetRotation), m_rotationSpeed); } } The unfortunate thing about this approach is that you can't specify the amount of time a rotation takes, only the incremental value of which the object will rotate by. An alternative approach would be to use Mathf.Clamp to add Time.deltaTime / rotationTime to the existing y rotation of the GameObject and stop once it's reached +/- 90. Hope this helps.

### Unity 2D Top Down Shooter movement issue C#

I am trying to make a simple 2D Top-Down Shooter in Unity. I have the basic movement and following of the mouse, but there is an issue in the movement. Whenever you press say Up and Right at the same time, it moves diagonally as you would expect, but when you let go of the Up key, it keeps going diagonally where I want it to move Right. My code for handling the movement is: private void Update () { if (Input.GetKey(Up)) { Debug.Log("UP"); Vector3 velUp = rigidbody2D.velocity; velUp.y = walkSpeed; rigidbody2D.velocity = velUp; } else if (Input.GetKey(Down)) { Vector3 velDown = rigidbody2D.velocity; velDown.y = walkSpeed*-1; rigidbody2D.velocity = velDown; } else if (Input.GetKey(Left)) { Vector3 velLeft = rigidbody2D.velocity; velLeft.x = walkSpeed*-1; rigidbody2D.velocity = velLeft; } else if (Input.GetKey(Right)) { Vector3 velRight = rigidbody2D.velocity; velRight.x = walkSpeed; rigidbody2D.velocity = velRight; } else { Vector3 velStop = rigidbody2D.velocity; velStop.x = 0; velStop.y = 0; rigidbody2D.velocity = velStop; } //rotation Vector3 mousePos = Input.mousePosition; Vector3 objectPos = Camera.main.WorldToScreenPoint (transform.position); mousePos.x = mousePos.x - objectPos.x; mousePos.y = mousePos.y - objectPos.y; float angle = Mathf.Atan2(mousePos.y, mousePos.x) * Mathf.Rad2Deg; transform.rotation = Quaternion.Euler(new Vector3(0, 0, angle)); } How can I get the movement to behave as I mentioned? With it moving diagonally like this makes the movement seem off. Any help is greatly appreciated. Thanks.

You start with a velocity of 0, then you add up all the movements directions you have. Then you normalize the vector and scale it to your movement speed. Otherwise the player moves faster, if walking diagonal. I haven't checked the code, but something like this will do: void Update () { Vector3 vel = new Vector3(); if(Input.GetKey(Up)){ Debug.Log("UP"); Vector3 velUp = new Vector3(); // just use 1 to set the direction. velUp.y = 1; vel += velUp; } else if(Input.GetKey(Down)){ Vector3 velDown = new Vector3(); velDown.y = -1; vel += velDown; } // no else here. Combinations of up/down and left/right are fine. if(Input.GetKey(Left)){ Vector3 velLeft = new Vector3(); velLeft.x = -1; vel += velLeft; } else if(Input.GetKey(Right)){ Vector3 velRight = new Vector3(); velRight.x = 1; vel += velRight; } // check if player wants to move at all. Don't check exactly for 0 to avoid rounding errors // (magnitude will be 0, 1 or sqrt(2) here) if (vel.magnitude > 0.001) { Vector3.Normalize(vel); vel *= walkSpeed; rigidbody2D.velocity = vel; } //rotation Vector3 mousePos = Input.mousePosition; Vector3 objectPos = Camera.main.WorldToScreenPoint (transform.position); mousePos.x = mousePos.x - objectPos.x; mousePos.y = mousePos.y - objectPos.y; float angle = Mathf.Atan2(mousePos.y, mousePos.x) * Mathf.Rad2Deg; transform.rotation = Quaternion.Euler(new Vector3(0, 0, angle)); } Regarding Normalize have a look at this image. If you walk only upwards or right you would move with speed 1 (which we later multiply with your desired speed). But if you walk diagonal you walk with about 1.4 times the desired speed (green vector). Normalize keeps the direction of the vector intact, but gives you a length (also called "magnitude") of 1 (red vector). In older shooters you may find a bug called "bunny hopping". I'm not sure if this is the source of the problem, but I would guess it is. Regarding vel.magnitude > 0.001: We actually want to know if vel.magnitude > 0. But vel.magnitude is the result of a calculation and thus may contain rounding errors. If you work with floating-point values always keep that in mind. The check itself is done because the Normalize method needs to divide by the magnitude and division by zero is evil. Not sure if Normalize checks for this itself.

It seems the only time you force your velocity to nothing is when there isn't any key being pressed. I might suggest adding some checks to see when Input.GetKeyUp is true, and set the rigidbody2D.velocity's x or y values to 0, maybe something like this: void Update () { if (Input.GetKeyUp(Up) || Input.GetKeyUp(Down)) { rigidbody2D.velocity.y = 0; } if (Input.GetKeyUp(Left) || Input.GetKeyUp(Right)) { rigidbody2D.velocity.x = 0; } ...