MouseLook conflicting with LookAt
Similar to @Broccoli 's question back in the day, however his solution is not working for me. My objective it to make camera appear slightly offset from a gameObject and look at it. I can accomplish this for 1 frame, but then the other mouse look functions in my MouseLook
script (attached to the camera) pull it away . The camera teleportation and rotation calculations are triggered by an external script NameTransfer
, that also toggles a boolean warpToNode
to true
once the camera teleports to the offset destination. Within the MouseLook script,`warpToNode`, when true
, triggers transform.LookAt(NameTransfer.desired_destination)
and the camera does indeed look at the gameObject, but as soon as warpToNode is set to false
the camera snaps away; even after I apply the rotationX = transform.localRotation.x;
and rotationY = transform.localRotation.y;
as recommended by @Aubrey Hesselgren. I've spent several hours trying different things; if anyone can be of assistance please let me know.
This is how the camera warps to the offset position near the gameObject, as executed in NameTransfer
script:
theMainCamera.GetComponent<Transform>().position = desired_destination += offsetVector;
And here's my LookAtNodeNicely()
function in the`MouseLook` script. It is a modified version of @godlikemouse 's Topology network visualization software (as described here: https://collaboradev.com/2014/03/12/visualizing-3d-network-topologies-using-unity/); I just added some lerps to smooth things out, and this function:
void LookAtNodeNicely()
{
if (NameTransfer.warpToNode == true)
{
//Make it look at the node's location
transform.LookAt(NameTransfer.desired_destination); // look at the Node we warped to
// Per Aubrey Hesselgren, this should prevent camera snapping back
rotationX = transform.localRotation.x;
rotationY = transform.localRotation.y;
// the only way I can get it to 'stick' is when I DO NOT set warpToNode = false at end,
// but that locks my rotation and I can't look around anymore
//Switch warpToNode off so we can repeat this process on other gameObjects
NameTransfer.warpToNode = false;
}
}
Answer by streeetwalker · Jun 05, 2020 at 04:31 PM
Doesn't the MouseLook script have an "original rotation" variable that it uses to base all other rotations on? Maybe try reseting the original rotation to the new orientation after your LookAt.
Answer by jzylinski · Jun 09, 2020 at 05:42 PM
Thank you for suggestion @streeetwalker , but unfortunately that did not fix things. Did I perhaps not set it to match the correct rotation variable? For context, here is the whole script:
[AddComponentMenu("Camera-Control/Mouse Look")]
public class MouseLook : MonoBehaviour {
public enum RotationAxes { MouseXAndY = 0, MouseX = 1, MouseY = 2 }
public RotationAxes axes = RotationAxes.MouseXAndY;
public float sensitivityX = 15F;
public float sensitivityY = 15F;
public float minimumX = -360F;
public float maximumX = 360F;
public float minimumY = -70F; //default was 60
public float maximumY = 70F; // default was 60
public float Snappiness = 0.125f; //JRZ factor mouse smoothing
float xAccumulator; // holds lerp version of input for x
float yAccumulator; // holds lerp version of input for y
float rotationX = 0F;
float rotationY = 0F;
bool warpToNode; // if user trigger
Quaternion originalRotation;
void Update ()
{
if (axes == RotationAxes.MouseXAndY)
{
// Read the mouse input axis
rotationX += Input.GetAxis("Mouse X") * sensitivityX;
rotationY += Input.GetAxis("Mouse Y") * sensitivityY;
// JRZ insert the lerps for smoothing the inputs
// If this breaks; replace all 'x|yAccumulator' vars below with rotationX|Y
xAccumulator = Mathf.Lerp(xAccumulator, rotationX, Snappiness * Time.deltaTime);
yAccumulator = Mathf.Lerp(yAccumulator, rotationY, Snappiness * Time.deltaTime);
xAccumulator = ClampAngle(xAccumulator, minimumX, maximumX);
yAccumulator = ClampAngle(yAccumulator, minimumY, maximumY);
Quaternion xQuaternion = Quaternion.AngleAxis(xAccumulator, Vector3.up);
Quaternion yQuaternion = Quaternion.AngleAxis(yAccumulator, -Vector3.right);
transform.localRotation = originalRotation * xQuaternion * yQuaternion;
}
else if (axes == RotationAxes.MouseX)
{
xAccumulator += Input.GetAxis("Mouse X") * sensitivityX;
xAccumulator = ClampAngle(xAccumulator, minimumX, maximumX);
Quaternion xQuaternion = Quaternion.AngleAxis(xAccumulator, Vector3.up);
transform.localRotation = originalRotation * xQuaternion;
}
else
{
yAccumulator += Input.GetAxis("Mouse Y") * sensitivityY;
yAccumulator = ClampAngle(yAccumulator, minimumY, maximumY);
Quaternion yQuaternion = Quaternion.AngleAxis(-yAccumulator, Vector3.right);
transform.localRotation = originalRotation * yQuaternion;
}
}
void Start ()
{
// Make the rigid body not change rotation
if (GetComponent<Rigidbody>())
GetComponent<Rigidbody>().freezeRotation = true;
originalRotation = transform.localRotation;
}
public static float ClampAngle (float angle, float min, float max)
{
if (angle < -360F)
angle += 360F;
if (angle > 360F)
angle -= 360F;
return Mathf.Clamp (angle, min, max);
}
void LookAtNodeNicely()
{
if (NameTransfer.warpToNode == true)
{
//Make it look at the node's location
transform.LookAt(NameTransfer.desired_destination);
originalRotation = transform.localRotation;
//NOW we can switch warpToNode off
NameTransfer.warpToNode = false;
}
}
I tried something else; I hashed-out the above LookAtNodeNicely()
function and put it in a separate script that disables all the other camera controls before perfor$$anonymous$$g a camera rotation. I can see it disabling the other camera input scripts when it executes, but it still doesn't rotate to the object.
public class NodeLook : $$anonymous$$onoBehaviour
{
//This disables move/look controls while the camera pans to the node
// Update is called once per frame
void Update()
{
if (NameTransfer.warpToNode == true)
{
//Disable the mouselook component
Camera.main.GetComponent<$$anonymous$$ouseLook>().enabled = false;
Camera.main.GetComponent<CameraControlZeroG>().enabled = false;
//Figure out how the camera needs to rotate
Quaternion rotTarget = Quaternion.LookRotation(NameTransfer.desired_destination - transform.position);
//Apply the rotation
transform.rotation = Quaternion.RotateTowards(transform.rotation, rotTarget, 50f * Time.deltaTime);
//After it's done returning, switch the warpToNode off
NameTransfer.warpToNode = false;
}
else if (NameTransfer.warpToNode == false)
{
Camera.main.GetComponent<$$anonymous$$ouseLook>().enabled = true;
Camera.main.GetComponent<CameraControlZeroG>().enabled = true;
}
}
}
Your answer
![](https://koobas.hobune.stream/wayback/20220613000157im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
How to rotate camera around player when idle, but control player rotations when moving? 0 Answers
Standard mouselook X-rotation clamp doesn't work with standard rigidbodyfirspersoncontroller 0 Answers
Mouse Look Right Click Bounce 0 Answers
Top down shooter gun point to cursor not working 0 Answers
Camera bobbing because of smoothing option. How to prevent it? 0 Answers