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
2
Question by Karsnen_2 · Dec 10, 2012 at 11:53 PM · c#rotationiosquaternion

[Solved] Help on a rotation issue for a Top-Down view?

Hello Devs,

I developing a Top-Down game where there are two different camera modes.

  1. Camera follows the Player on a Top-Down view basis.

  2. The camera suddenly stops moving and just looks at the player from it's position.

Now there might be a lot of transition between the two modes. During both the modes, I would not like the player not to get away from the screen AND I would like to have a lot of fluidity between those transitions.

On a Top-Down view the most common eulerangles is (90, 0, 0).

CODE SETUP :

  • I have a switch case to switch between those two modes, whenever needed.

  • Each of the two cases, have two common variable, which is "Target" position/rotation.

  • I have two common function for both of the cases, which lerps the postion and rotation from "this.transform"position/rotation to the "Target" position/rotation.

  • As the player has to be within screen at all times, I use a funtion `Quaternion.LookRotation(PlayerPosition - this.transform.position);`

CODE :

 void Update ()
 {
     DeltaTime = Time.deltaTime;
     switch (CurrentMode)
     {
     case CameraMode.FollowPlayer :
         TargetPosition = new Vector3(PlayerPosition.x, (PlayerPosition.y + HeightFromTarget), PlayerPosition.z);    
         TargetRotation = RequiredRotation();
         break;
         
     case CameraMode.LookAtPlayer :
         TargetPosition = this.transform.position;
         TargetRotation = RequiredRotation();
         break;
     default:
         break;            
     }
     this.transform.position = Vector3.Lerp(this.transform.position, TargetPosition, DeltaTime/2);
     this.transform.rotation = Quaternion.Lerp(this.transform.rotation, TargetRotation, DeltaTime * 50);
 }
 
 Quaternion RequiredRotation()
 {
     RotationalVector = (PlayerPosition - this.transform.position);
     return Quaternion.LookRotation(RotationalVector);            
 }


PROBLEM:

The rotation seems to work fine, except for the mode where the camera is on the Top-Down view following the player. It gets a little messy as the camera suffers rotation on the Y axis which just confuses and makes things very complicated. I would like to find a solution to that.

SCREENSHOT:

During one frame on Top-Down view : alt text

During another frame on Top-Down View : alt text

During the camera being stationary and looking at the player : alt text

CRITERIA :

  • I have to use the common Lerp function which could be found at the end of the Update function.

  • I can't manually use the function Quaternion.Euler(90, 0, 0) for Top-Down view.

  • The most common need is that, the camera should face the player at all times and not suffer a confusing Y rotation during the Top-Down view.

I would appreciate your help, as always. Thank you.

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

1 Reply

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

Answer by Bunny83 · Dec 11, 2012 at 12:28 AM

Well, the answer why this is happening is quite simple:

LookRotation and Transform.LookAt require more than a direction / position to look along / at in order to create a rotation. You didn't specified the upvector, so it defaults to "world up" (simply Vector3.up). That means LookRotation will make the z axis to point exaxtly along your direction vector, but the rotation around this vector is determined by the up vector.

Usually the camera should have up == world up, but if you go into the extreme and tip over, the camera would be upside down, that's why the rotation suddenly turns around to still have the upvector pointing upwards. You can "fix" the rotation by providing your own up vector which of course doesn't fix point upwards. How you calculate it is up to you. There's no general way to do this because it depends on what behaviour you want.

Comment
Add comment · Show 1 · 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 Karsnen_2 · Dec 11, 2012 at 03:30 PM 0
Share

Well it did work. Thanks. I totally forgot the second paramater on LookRotation function. I kind supplied the normalized difference vector between the player and the camera. It worked.

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

10 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

Related Questions

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

Help regarding World-Local Transfrom Rotation? 1 Answer

How good is my "Quaternion" code efficiency? 1 Answer

Flip over an object (smooth transition) 3 Answers

How to make a plane face a static but rotating camera? 3 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