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 /
This question was closed Oct 09, 2015 at 04:22 PM by Tyrrrz for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by Tyrrrz · Sep 27, 2013 at 10:19 PM · cameracontrol

Doing a proper 180 smooth turn

Hello. I'm writing a first person game in which i want to implement an ability to do a 180 degree turn when a button is pressed.

I have this script to do it so far:

     public Quaternion playerStartRot;
     public Quaternion playerEndRot;
     public float time;
     
     public bool working = false;
     
     private GameObject player;
     
     IEnumerator SmoothRotate ()
     {
         float i = 0;
         float rate = 1/time;
         while (i < 1) 
         {
             i += Time.deltaTime * rate;
             player.transform.rotation = Quaternion.Slerp (playerStartRot, playerEndRot, i);
             
             yield return 0;
         }
         working = false;
     }
     
     void Start ()
     {
         player = transform.parent.gameObject;
     }
     
     void Update ()
     {
         if (working)
             StartCoroutine(SmoothRotate());
     }

And here's the part of script that invokes this one:

 if (Input.GetButton("TurnAround"))
         {
             var camOverride = cam.GetComponent<Player_CameraOverride>();
             if (camOverride != null && !camOverride.working)
             {
                 camOverride.playerStartRot = transform.rotation;
                 camOverride.playerEndRot = transform.rotation * Quaternion.AngleAxis(180, transform.up);
                 camOverride.time = .2f;
                 camOverride.working = true;
             }
         }

Camera is a child of the Player object. In my camera control script i turn my player object around horizontally and camera vertically, as a result making the whole object turn horizontally (since camera is a child) and camera also turning vertically in its own space. I also freeze that script for the 180 turn so it wont interfere.

At the moment, this script turns my player model 180 degrees horizontally but also resets camera's vertical rotation. I now want to keep the camera rotation the way it was, because currently it resets to 0,0,0 when the script starts. I've tried many ways to do it, but, apparently im horribly awful with Quaternions and just cant to understand them.

Whats the right way to do it?

Also, my camera seems to flicker when i press the button a lot of times, despite the "working" bool check in the invoke.

Comment
Add comment · Show 3
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 Tyrrrz · Sep 28, 2013 at 02:39 PM 0
Share

Thanks a lot for helping me with the flickering, i dont know what i expected putting it into Update (). $$anonymous$$ust've been late at night. Anyway, the other problem was occurring because i was incorrectly freezing the camera move script (the turn values werent restored properly on new rotation) so the X axis was flickering. You can add your answer about startcoroutine in update () so i can accept it.

avatar image Eric5h5 · Sep 28, 2013 at 02:49 PM 0
Share

A small issue, but use yield return null rather than yield return 0, since the former has no allocations.

avatar image Tyrrrz · Sep 28, 2013 at 04:38 PM 0
Share

@Eric5h5 fixed, thanks

1 Reply

  • Sort: 
avatar image
0
Best Answer

Answer by robertbu · Sep 28, 2013 at 04:00 AM

Your flicker issue is to to line 30 and 31. A StartCoroutine() is called just once. You are calling it every update during the time the rotation is running. Depending on your frame rate, during the .2 seconds you are stacking up between 5 and 25 coroutines. Fortunately they are all doing the same thing.

As for your rotation issue, I don't understand it from your description. I'd have to see the camera code, and perhaps a video or play with a sample project. If I can understand the issue, a fix should be easy.

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

Follow this Question

Answers Answers and Comments

16 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

Related Questions

How to make camera position relative to a specific target. 1 Answer

Help with camera coding - Modern Zelda-style camera 1 Answer

GUI controlling other game objects 0 Answers

Help Ship Controll 0 Answers

Player controller 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