Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by bpercevic · Sep 17, 2017 at 11:28 PM · rotationincrementfixed

Having trouble rotating camera around player in 180° increments

I have a sidescrolling 2D game in which you can rotate the world by 90° or 180°. The result is that I can connect a bunch of corridors along the X and Z grid, and rotate 90° at their intersections to allow you to switch between the corridor along the X axis, and the one along the Z axis. I want each rotation to be lerped instead of instant.

90° rotation works like a charm. If I press the positive rotation button the world rotates +90°, and -90° with the negative rotation button. I can keep pressing a direction and the world always rotates in that direction.

But 180° rotation doesn't work, regardless of which button (positive or negative) I press, it rotates once +180° and the next -180°, then +, then -, then +, and so on. I would like for this rotation to also work like the 90° rotation so that it always rotates in the direction I'm pressing rather than alternating.

I suspect it has something to do with either how I wrote the transform, or based on things I've read in the forum about the engine selecting the shortest path from the current rotation to the target rotation, but this is my first venture into game development so it's likely that my transform is the culprit, haha. Here's the troubling code:

 // I have a player GameObject that has a camera as a child. I move and rotate the player
 // only, without touching the camera itself. This is the hierarchy
 //
 //   Player
 //   -- Camera
 //
 
 // This script is on the player. This excerpt is the rotation code only,
 // I think movement is irrelevant and works fine.
 void Update () {
   if (Input.GetButtonDown ("Camera Rotation")) {
     float rotationDirection = Input.GetAxisRaw ("Camera Rotation");
 
     if (rotationDirection < 0) {
       targetAngle += 180f; // or 90f depending on some condition being met
     } else {
       targetAngle -= 180f; // or 90f, again, based on same condition
     }
 
     // Basically what happens above is I check which direction I pressed,
     // and whether it should rotate by 180° or 90°. The code below is what
     // I think I'm doing wrong
   }
 
   // Again, this.transform is the player's transform
   this.transform.eulerAngles = new Vector3( 0, 
                                             Mathf.LerpAngle(this.transform.eulerAngles.y,
                                             targetAngle,
                                             rotationSpeed * Time.deltaTime),
                                             0);
 }

Any help is appreciated!

TLDR: Right bumper rotates world by n°, Left bumper rotates world by -n°, and should do so for as many times as I press the bumper. Rotation by 90° works great, but rotation by 180° rotates once left, then right, and so on every time I press a bumper.

Thank you in advance for anyone who is willing to help me out! :)

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

0 Replies

· Add your reply
  • Sort: 

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

140 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 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 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 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 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 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 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

Rotating Skybox Issues? 1 Answer

How to set clockwise or counterclockwise rotation? 0 Answers

2d keep player upright 1 Answer

Object rotate and slowly stop to toward 1 Answer

Applying AddForce() while rotating player gameobject. 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