Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
4 captures
13 Jun 22 - 14 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 /
avatar image
0
Question by JeffreyBennett · Feb 27 at 06:45 PM · camerajumpcamera-movementcamera rotateconflict

How to Set Variable to Camera's Current Local Rotation After Movement

ISSUE Here's what I'm trying to make in a 3D game: 1. The player can rotate the camera with the mouse to look around. 2. The player can see an item in the distance. 3. The player can click on the item in the distance. 4. The player flies over to that item, landing in front of it and looking at. 5. The player can look around some more as in Step 1.

PROBLEM All of that is working ok. Here's the problem, in the transition from Step 4 to Step 5, when the player has landed and is looking at the object, and first clicks the mouse, the camera "jumps" and looks in the direction it was looking just before "flying" over to the object.

HOW I'M DOING THIS I have two scripts: - A script to let the player drag the mouse and look around. - A script to let the player click on the object, which makes the player fly over there: - Use a raycast to detect the distant object. - Use transform.position to fly (lerp) to the distant object. - Use transform.LookAt to turn the camera and look at the (no longer distant) object.

WHAT I THINK IS GOING ON The two scripts are in conflict about which way the camera is pointed:

  • The "look-around" script thinks the camera is pointed at whatever it was pointed at just before the distant object was selected, and this is no longer true.

  • The "fly-over-and-look-at-it" script thinks it is looking at the object, and it is correct.

When you try to drag the mouse again to "look around" that script jumps the camera back to the position it was in before moving it around. This causing a jarring visual effect.

WHAT I'VE TRIED TO FIX THIS I would think I could tell the "look around" script, "Hey! The position of the camera has changed. You need to get the camera's current rotation, then rotate it FROM THAT SETTING. NOT from the setting you last recorded."

That approach is not working. What am I missing?

Can anyone tell me the mistake I'm making?

Here is my "Look around Script" public class MouseDragRotCamInXY : MonoBehaviour { [SerializeField] private float mouseDragSpeed = 1.0f; [SerializeField] private float maxAngleUp = 45; [SerializeField] private float maxAngleDown = -45; [SerializeField] private float dragInverterX = -1; [SerializeField] private float dragInverterY = 1;

     public bool somethingElseIsWorking;
     private Quaternion _camRotation;
     void Start()
     {
         _camRotation = transform.localRotation;
     }
     
     
     void Update()
     {
         if (!somethingElseIsWorking)
         {
             if (Input.GetMouseButton(0))
             {
                 if (_camRotation != transform.localRotation)
                 {
                     _camRotation = transform.localRotation; //This line does NOT seem to do what I think it would do.
                 }
                 _camRotation.x += Input.GetAxis("Mouse Y") * mouseDragSpeed * dragInverterX;
                 _camRotation.y += Input.GetAxis("Mouse X") * mouseDragSpeed * dragInverterY;
 
                 _camRotation.x = Mathf.Clamp(_camRotation.x, maxAngleDown, maxAngleUp);
                 
                 transform.localRotation = Quaternion.Euler(_camRotation.x, _camRotation.y, _camRotation.z);
             }
         }
         else
         {
             _camRotation = transform.localRotation;
         }
         
     }
     
 }

Here is my "Fly Over There" script: public class MouseClickMovesCamera : MonoBehaviour {

 [SerializeField] private string selectedObjTag = "MapPin";
 [SerializeField] private string colliderTag;
 [SerializeField] private float minDistX = 0.1f;
 [SerializeField] private float minDistY = 0.1f;
 [SerializeField] private float minDistZ = 0.1f;
 [SerializeField] private float speed = 0.0f;
 [SerializeField] private bool camNeedsToMove = false;
 
 private GameObject moveTarget;
 private GameObject lookTarget;
 
 [SerializeField] private Vector3 startPosition = new Vector3(20, 20, 20); // This is where the camera will start
 [SerializeField] private Vector3 framedPosition; // This is where the camera will move when object is clicked
 
 
 // Update is called once per frame
 void Update()
 {
     //Mouse Click happens
     if (Input.GetMouseButtonDown(0))
     {
         
             //Create variables to cast a ray from the camera to wherever the mouse clicked
             RaycastHit hit;
             Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
             //Debug.DrawRay(ray.origin, ray.direction * 500, Color.red, 2.0f);
 
             //Detect if ray hit object
             if (Physics.Raycast(ray, out hit, 1000)) //Will need to test whether all map pins can be hit by a ray of this length (1000 units) from anywhere inside the villa
             {
                 
                 //Get the tag of whatever you hit
                 colliderTag = hit.collider.tag;
                 
                 //If what you hit was a map pin then you'll need to move the camera
                 if (colliderTag == selectedObjTag)
                 {
                     moveTarget = hit.transform.GetChild(0).gameObject;
                     lookTarget = hit.transform.GetChild(1).gameObject;
                     colliderTag = "";
                     camNeedsToMove = true;
                 }
             }
     }
     
     if (camNeedsToMove)
     {
          MoveTheCam();               
     }
 }

 public void MoveTheCam()
 {
     //Capture the Camera's current position
     startPosition = Camera.main.transform.position;
     framedPosition = moveTarget.transform.position;

     //If the Camera has not moved to its new position
     if ((Mathf.Abs(framedPosition.x - Camera.main.transform.position.x)) > (minDistX) ||
         ((Mathf.Abs(framedPosition.y - Camera.main.transform.position.y)) > (minDistY) ||
          ((Mathf.Abs(framedPosition.z - Camera.main.transform.position.z)) > minDistZ)))
     {
         //Move the camera into position
         Camera.main.transform.position = Vector3.Lerp(startPosition, framedPosition, speed);
         Camera.main.transform.LookAt(lookTarget.transform.position);
         Debug.Log("MapPin is moving the camera: " + Camera.main.transform.position);

     }

     else
     {
         camNeedsToMove = 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

0 Replies

· Add your reply
  • Sort: 

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

207 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 avatar image avatar image

Related Questions

Why is my camera not snapping 90 degrees? 0 Answers

change objects Axis to match cameras rotation 0 Answers

How to make slight Camera move and smooth 0 Answers

Allow both turning of the mouse and turning of the player to keep the camera pointing towards the player 1 Answer

How can I mimic a Camera from a game to my project? 1 Answer


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