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 /
avatar image
0
Question by mntng · Aug 01, 2013 at 03:05 PM · c#cameraraycastlookatzoom

Camera rotate to look at GameObject from Raycast

Hi guys,

I'm trying to write a C# script that zooms a stationary camera via scroll wheel. I want it so that when a scrolling motion is detected, if the mouse position is over a game object, then the camera rotates towards the game object to look at it and zoom in/out. I'm using Physics.Raycast and having a few problems with accessing the position of the object that is hit for camera.transform.LookAt...

My code so far:

 private const int ZoomSpeed = 7; 
 
     void Update() {    
     
         if(Input.GetAxis("Mouse ScrollWheel") != 0) {
             Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
             RaycastHit hit;
                     if(Physics.Raycast(ray,out hit, Mathf.Infinity)) {
                 camera.transform.LookAt (hit.collider.gameObject.position); 
                 //This doesn't work
             }
         }
             
         var zoomDelta = Input.GetAxis("Mouse ScrollWheel")*ZoomSpeed*Time.deltaTime;
         if (zoomDelta > 0 && Camera.main.fieldOfView >10){
             Camera.main.fieldOfView--;
         }
         if (zoomDelta < 0 && Camera.main.fieldOfView <60){
             Camera.main.fieldOfView++;
         }
     }

Any suggestions appreciated!

Comment
Add comment · Show 1
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 mntng · Aug 01, 2013 at 03:39 PM 0
Share

camera.transform.LookAt (hit.collider.gameObject.transform); worked! (Thank you!)

Follow-up question: I'm wondering if there's a way to get the camera to zoom in/out to any spot that the mouse is over (not necessarily at game object)?

3 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by Seizure · Aug 01, 2013 at 03:26 PM

Try using:

camera.transform.LookAt (hit.collider.gameObject.transform);

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
1

Answer by mntng · Aug 02, 2013 at 02:55 PM

Ok, here is the final version of the zoom to mouse cursor script via scroll wheel by using Lerp... This script tries to zoom towards/around the mouse cursor so that the mouse is still on the object/area it was initially pointing at (a bit like Google Maps zoom). The Lerping out is still a little buggy, however. Note that the values are tailored to my game and it sometimes shifts a little off the mouse:

 Quaternion init; 
     void Start() 
     {
         init = camera.transform.rotation;
     }
 
     void Update () {
             if (Input.GetAxis("Mouse ScrollWheel") < 0) 
             {
                 camera.transform.rotation = Quaternion.Lerp(init, camera.transform.rotation, .3f);
             }
             else if (Camera.main.fieldOfView < 50 && Input.GetAxis("Mouse ScrollWheel") < 0)
             {
                 var startR = camera.transform.rotation;
                 
                 camera.fieldOfView -= 10*Input.GetAxis("Mouse ScrollWheel");
                 camera.transform.rotation = Quaternion.Lerp(init, startR, .7f);
             
           }
             else if (Camera.main.fieldOfView > 7 && Input.GetAxis("Mouse ScrollWheel") > 0)
             {
                 var currentPos = Input.mousePosition;
                     currentPos.z = 7.3f;
                     var startR = camera.transform.rotation;
                 
                 camera.transform.LookAt(camera.ScreenToWorldPoint(currentPos));
                 camera.fieldOfView -= 10*Input.GetAxis("Mouse ScrollWheel");
                 camera.transform.rotation = Quaternion.Lerp(transform.rotation, startR,((float)Camera.main.fieldOfView -(float)20*Input.GetAxis("Mouse ScrollWheel")) /((float)Camera.main.fieldOfView) );
             
             }
     }

Thanks for all the help!

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 FuzzyLogic · Aug 01, 2013 at 04:19 PM

Regarding your followup question:

You can get the world position under the mouse cursor by using...

 worldPos = camera.ScreenPointToRay(mousePosition).GetPoint(distanceInWorldUnits);

As you can see, you need to provide a distance from the camera for the 3D mouse position. This is because the mouse position is 2D screen space and obviously the world space is 3D. A distance of 0 would put your 3D point on the cameras near plane. How to determine the distance you want the 3D point to be is up to you.

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

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

16 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

Related Questions

Make the terrain ignore Raycast if in between Camera and Player? 1 Answer

Pinch zoom 0 Answers

How to move the camera closer to the player while there is an object between them? 1 Answer

Mouseclick GameObject through the view of a RenderTexture 0 Answers

How to turn camera using LookAt() only along X and Y axis? 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