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 boddole · Jan 18, 2014 at 10:25 AM · c#camera rotate

Lerping Camera Rotation in Local Space

Hello everyone, I've been trying to setup a 1st person camera controller that allows players to (among other things) rotate the camera between fixed positions (and lerp to that position and lerp back from that position).

The problem I'm having is that I have been unsuccessful in getting the camera's rotation to be in local space, so say I want a button to rotate the camera left 30 degrees, instead of 30 degrees from center in local space, it always goes to 30 in world space and creates obvious problems.

I've tried a few solutions, but now I think I'm just making myself more confused. Any ideas are appreciated.

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 MrVerdoux · Jan 18, 2014 at 12:44 PM

I use RotateAround for such things.

http://docs.unity3d.com/Documentation/ScriptReference/Transform.RotateAround.html

 transform.RotateAround(transform.position, transform.up, someAngle);

After reading your comments I´m getting the feeling that I did understand your question, so I´ll try to explain this method further.

Let´s say your camera is at (100,232,-10), which are random numbers I just made up. Generally you don´t want to rotate the camera around (0,0,0), but around its own position, which is (100,232,-10). Now, that position is (0,0,0) in its own coordinates system, how would you do it? With rotate around. In a smooth way, you could create a script like this:

 public class SmoothCamera : MonoBehaviour {
 
     public float targetAngle = 0;
     public float currentAngle = 0;
     public float minimumDifference = 0.0001f;
     public float speed = 10f;
 
     void FixedUpdate () {
         if (Mathf.Abs(targetAngle-currentAngle) > minimumDifference)
             RotateLocaly(Mathf.Sign(targetAngle-currentAngle)*speed*Time.fixedDeltaTime);
     }
 
     void RotateLocaly(float angle){
         transform.RotateAround(transform.position, new Vector3(0,1,0), angle);
         currentAngle += angle;
     }
 }
 

Simply add it to a camera and it will look to the angle that you set at targetAngle. And it rotates locally and smothly.

Comment
Add comment · Show 3 · 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 boddole · Jan 18, 2014 at 02:18 PM 0
Share

I've tried transform.Rotate, but then I ended up with trouble trying to clamp the resulting rotation.

avatar image MrVerdoux · Jan 18, 2014 at 02:41 PM 0
Share

Therefore try with RotateAround, so you can specify where are the axis of rotation located.

avatar image boddole · Jan 18, 2014 at 02:53 PM 0
Share

Yeah, but then you still have the problem of clamping the angle with the additional problem of clamping position. I guess I just don't see why RotateAround would be useful in this case.

--It occurs to me you might have taken lerping as a desire to move the camera's position and its rotation. I'm just looking for rotation (the equivalent of turning your head left/right/up/down).

avatar image
0

Answer by Map-Builder · Jan 18, 2014 at 03:09 PM

Qa = rotation of the Relative object you want a Qb rotation according to the Qa

 Qfinal = Qa * Qb; // so your object of rotation Qf has been rotated by Qa and the Qb.

if you do not know Qb :

     Qb = Qf * Quaternion.Inverse(Qa); // this is the formula to get the difference between rotation A and F.
Comment
Add comment · Show 3 · 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 Gruffy · Jan 18, 2014 at 03:32 PM 0
Share

Listen, I know this isnt an answer, but the method you are looking to invoke is Quaternion.Slerp. You would be specifying the transform.localRotation as its start point for example and then another transform`s localRotation as it`s end point, then an originial identity like transform.position or Quaternion.identity to ensure it is the base rotational direction of the transform your are moving.

Check out these references to see what i mean and find out more about quaternion rotations.

[SLERP with Quaternions][1] [LERP with quaternions][2] [This link is for three LookRotation methods available and actually are designed to deal with your issue][3]

Hope that helps in some way. Take care & thanks for reading Gruffy [1]: http://docs.unity3d.com/Documentation/ScriptReference/Quaternion.Slerp.html [2]: http://docs.unity3d.com/Documentation/ScriptReference/Quaternion.Lerp.html [3]: http://docs.unity3d.com/Documentation/ScriptReference/30_search.html?q=Quaternion.LookRotation

avatar image boddole · Jan 19, 2014 at 02:01 AM 0
Share

Thank you $$anonymous$$ap Builder and Gruffy for your answers. I tried your suggestions Gruffy and I always end up with 1) the position changing instantly, regardless to time, and 2) I have to clamp the rotation and I always end up with conversion errors.

I think I'm just don't know enough to get this done, and need to go look for some primers on Quaternions, if you guys have seen any good resources that would be great.

avatar image boddole · Jan 19, 2014 at 02:44 AM 0
Share

Think I might finally have something I'm not completely embarrassed to show: (so this just goes from a center position, to the "left" position, then back to center again). Am I on the right track?

 using UnityEngine;
 using System.Collections;
 
 public class Camera$$anonymous$$ovement : $$anonymous$$onoBehaviour {
     public Transform defaultRotation;
     public Transform leftRotation;
 
 
 
     void Update ()
     {
         $$anonymous$$oveCamera ();
     }
 
     void $$anonymous$$oveCamera ()
     {
 
         if (Input.Get$$anonymous$$ey ($$anonymous$$eyCode.Y))
         {
             Vector3 relativePos = (leftRotation.position - transform.position);
             Quaternion rotation = Quaternion.LookRotation (relativePos);
 
             Quaternion current = transform.localRotation;
 
             transform.localRotation = Quaternion.Slerp (current,rotation, Time.deltaTime * 10.0f);
 
         }
 
         else
         {
             Vector3 relativePos = (defaultRotation.position - transform.position);
             Quaternion rotation = Quaternion.LookRotation (relativePos);
             
             Quaternion current = transform.localRotation;
             
             transform.localRotation = Quaternion.Slerp (current,rotation, Time.deltaTime * 10.0f);
         }
     }
 
 }

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

21 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

Related Questions

Multiple Cars not working 1 Answer

A node in a childnode? 1 Answer

Distribute terrain in zones 3 Answers

More ways to use Trail Renderer 1 Answer

Restricition of Objects. 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