• 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
1
Question by lvictorino · Jun 19, 2012 at 10:01 AM · camerarotationquaternion

How to force camera rotation?

Very simple here, I know... But I think I'm lost.

I've coded a script to control my camera and here is what I do to handle mouse control:

 void LateUpdate() {
     
     if (camlock == false) {
         _x += Input.GetAxis("Mouse X") * xSpeed * 0.02f;
         _y -= Input.GetAxis("Mouse Y") * ySpeed * 0.02f;
         
         Quaternion rotation = Quaternion.Euler(_y, _x, 0);
         
         _myTransform.rotation = rotation;
     }
 }

It works pretty fine. But when player will press a button I'd like to force the camera position and orientation. I've tried using:

     transform.rotation = Quaternion.LookRotation(my_new_rotation_vector3);

But the rotation is not good (due to the lateUpdate method computing _x and _y according to Input.GetAxis() I think).

Any idea on how I should do to force camera rotation? should I use quaternions? If yes how?

Thank you.

Comment
Add comment · Show 4
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 whydoidoit · Jun 19, 2012 at 10:19 AM 0
Share

So do you have a vector you want the camera to look at or an actual orientation?

avatar image lvictorino · Jun 19, 2012 at 10:31 AM 0
Share

I must admit that I don't know the difference between both. But yes, I have a vector3 defining where the camera has to look.

avatar image whydoidoit · Jun 19, 2012 at 10:36 AM 0
Share

And the Vector3 you have is a position not the description of the rotation around 3 axes?

Quaternion.LookRotation() looks down a vector which is a direction.

avatar image lvictorino · Jun 19, 2012 at 11:02 AM 0
Share

Yes it's a position not a rotation around axis.

1 Reply

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

Answer by whydoidoit · Jun 19, 2012 at 10:37 AM

This will modify the current camera rotation by the mouse, allowing you to set another rotation. Drop your _x and _y variables.

  void LateUpdate() {
 
     if (camlock == false) {
        var rot = transform.rotation.eulerAngles;
        rot.x += Input.GetAxis("Mouse X") * xSpeed * 0.02f;
        rot.y -= Input.GetAxis("Mouse Y") * ySpeed * 0.02f;
 
        _myTransform.rotation = Quaternion.Euler(rot);
     }

 }
Comment
Add comment · Show 2 · 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 lvictorino · Jun 19, 2012 at 11:01 AM 0
Share

Thanks. I'm a bit ashamed it was so obvious and I didn't think of it.

avatar image whydoidoit · Jun 19, 2012 at 11:02 AM 0
Share

We all do that kind of thing, all the time :)

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Storing camera angles for a FPS -1 Answers

Trouble with Camera Rotation Math 2 Answers

Transition current camera rotation to 0,0,0 1 Answer

allows users to view 360º content without a VR headset 0 Answers

Camera rotation the same as player rotation 1 Answer

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges