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
6
Question by Caiuse · Mar 10, 2011 at 03:59 PM · camerarotationlookatmouseorbitscroll-wheel

Changing The Camera Position & Offsetting LookAt

Zoomhelp diagram

Basically the illustration demonstrates what I'm trying to achieve, It may not even be a case of changing the LookAt Target, but more changing the camera's rotation to face the "top" on my GameObject, modifying the MouseOrbit.js.

In short: On zooming in using the mouse scrollwheel I want the camera to slightly rotate to focus on the top of the object, but at the same time be able to freely rotation around the same LookAt Target, Just like Unity's in built MouseOrbit.js

var target : Transform; //distance vars var distance = 40.0; var minDistance = 3.0; var maxDistance = 40.0; var zoomDistance = 3.0;

function LateUpdate(){

 var lookAt = target.position;
 var lookAtTop = target.position + Vector3.up;

 var position = Vector3(0.0, 0.0, -distance) + lookAt;
 transform.position = position;

 if (camera) {
     distance -= Input.GetAxis("Mouse ScrollWheel") * Time.deltaTime * Mathf.Abs(distance);
     distance = Mathf.Clamp(distance, minDistance, maxDistance);
 }

}

Comment
Add comment · Show 9
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 AngryOldMan · Mar 10, 2011 at 04:40 PM 0
Share

voted up for neat little illustration. I think you could change the lookat target to an empty game object which is set to the top of your current lookat target, or there's probably a way to script an offset of the current lookat target. Havn't answered because your scripts a bit to hectic for me to take a crack right now but if my comment is not clear and your still in need of help later comment back and I'l try demonstrate/script an example of what I mean :)

avatar image Caiuse · Mar 10, 2011 at 04:53 PM 0
Share

Thanks for the feedback and the vote up :D I was in two $$anonymous$$ds about pasting the whole script up! I really hate seeing other peoples jumble of script, but I felt I needed a direct modification of my own revision of the $$anonymous$$ouseLook.js. An example would be great. Thanks a lot for your time!

avatar image AngryOldMan · Mar 12, 2011 at 01:20 AM 0
Share

still working on this one it's trickier than I first expected. in the mean time take a look at trying to offset your lookat target on the y value

http://unity3d.com/support/documentation/ScriptReference/Transform.LookAt.html

http://unity3d.com/support/documentation/ScriptReference/Quaternion.LookRotation.html

avatar image Caiuse · Mar 12, 2011 at 11:29 AM 0
Share

var targetPos = target; var offset: Vector3 = transform.position - targetPos; transform.LookAt(transform.position + offset);

Offsets the LookAt backwards, that's how far I've got.

avatar image AngryOldMan · Mar 14, 2011 at 12:32 PM 1
Share

have you also tried transform.position + Vector3.up?

Show more comments

2 Replies

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

Answer by Scribe · Mar 19, 2011 at 01:33 PM

var maxview = 60; //the maximum field of view of the camera var minview = 10; //the minimum field of view of the camera var target1 : Transform; //the shape your looking at var maxOffset = 1; //the maximum offset to look at the top of the shape var minOffset = 0; //the normal Y offset var Offset = 0.00; //the offset of the camera 2 decimal places for smooth curve

 function Update () {    
     if (Input.GetAxis("Mouse ScrollWheel") < 0) { //if scroll is less than 0
             Camera.main.fieldOfView += 1; //add field of view (increase sight
             Offset -= 0.02; //number = maxOffset/(maxview - minview);
             if(Camera.main.fieldOfView >= maxview){
                 Camera.main.fieldOfView = maxview; //clamp to max field of view

             }
             if(Offset <= minOffset){
                 Offset = minOffset; //decrease offset until min offset
             }

         }

         if (Input.GetAxis("Mouse ScrollWheel") > 0) { //if scroll is more than 0
             Camera.main.fieldOfView -= 1; //decrease field of view (zoom)
             Offset += 0.02; //number = maxOffset/(maxview - minview);
             if(Camera.main.fieldOfView <= minview){
                 Camera.main.fieldOfView = minview;
             }
             if(Offset >= maxOffset){
                 Offset = maxOffset;
             }
         }
         var target2 = target1.position + Vector3(0, Offset, 0);

 transform.LookAt(target2); //look at target + offset

}

hope this helps put this on your camera

Scribe

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 Statement · Mar 16, 2011 at 03:08 PM

Apologies for not reading through all of your code. Can you not just lerp the look-at target and camera height as you come closer to the object? Alternatively you can set a position the object should attempt to move toward if you want a smoother movement.

var lookAt = target.position; var lookAtTop = target.position + Vector3.up; // change this to whatever you like

var factor = Mathf.Clamp01(distance / zoomDistance); lookAt = Vector3.Lerp(lookAtTop, lookAt, factor); position.y = Mathf.Lerp(lookAtTop.y, lookAt.y, factor) / 2.0f;

transform.LookAt(lookAt);

Comment
Add comment · Show 7 · 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 Caiuse · Mar 16, 2011 at 03:26 PM 0
Share

Thanks for the reply Statement I will give this solution a try later tonight and get back to you.

avatar image Caiuse · Mar 17, 2011 at 11:53 AM 0
Share

this has thrown me "The best overload for the method 'UnityEngine.$$anonymous$$athf.Lerp(float, float, float)' is not compatible with the argument list '(UnityEngine.Vector3, UnityEngine.Vector3, float)'." although the scripting reference clearly states (UnityEngine.Vector3, UnityEngine.Vector3, float)

avatar image Statement · Mar 17, 2011 at 12:07 PM 0
Share

Oh right, it should be the y components of the vectors in the code. Let me update quickly.

avatar image Statement · Mar 17, 2011 at 12:08 PM 0
Share

I am using $$anonymous$$athf.Lerp for the Y component, and was using vectors as input when I wanted floats.

avatar image Caiuse · Mar 17, 2011 at 12:12 PM 0
Share

Sorry statement i'm a bit confused, I've edited my main post to show you what I'm using so far. I can't quite grasp how I can implement the mathf.lerp

Show more comments

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

No one has followed this question yet.

Related Questions

Player rotates with camera(face same direction as the camera) 1 Answer

Trying to have object spin within player and always face "up" relative to camera [example pics of what I'm trying to achieve in post]] 0 Answers

Need help implementing a zoom function 0 Answers

Rotate camera 360 degrees? 1 Answer

An alternative to RotateAround for collisions 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