Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by jzylinski · Jun 05, 2020 at 03:23 PM · mouselookcamera-looktransform.rotationmouselock

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;
         }
     }
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
0

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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;
         }
     }
Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image jzylinski · Jun 09, 2020 at 06:45 PM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

205 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges