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 /
avatar image
0
Question by Ledifen · Jun 02, 2018 at 09:48 PM · rotationresetfps controllerplaymodescene view

Keep the scene view rotation of my camera in play mode (it resets to 0) [EXAM ^^]

I have a script for the camera movement (FPS) that works perfectly. The thing is, in my scene view, I place my player and the camera so I have a beautiful establishing shot for my game but when I hit play, the rotation of the player and the camera resets to zero, so I start the game with a... not so nice first image. Any idea ?

Here's the (only) script that takes charge of the rotation of my camera and my capsule.

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class ControllerCamera : MonoBehaviour {
 
     public float sensitivity = 5.0f;
     public float smoothing = 2.0f;
 
     Vector2 camLook;
     Vector2 smoothVector;
 
     GameObject characterCapsule;
 
     void Start () {
         characterCapsule = this.transform.parent.gameObject;                                                            
     }
 
     void Update () {
         
         var md = new Vector2 (Input.GetAxisRaw ("RightStick_X"), Input.GetAxisRaw ("RightStick_Y"));                    
 
         md = Vector2.Scale (md, new Vector2 (sensitivity * smoothing, sensitivity * smoothing));                        
         // Lerp allows the value to change smoothly (no abrupt stop nor start);
         smoothVector.x = Mathf.Lerp (smoothVector.x, md.x, 1f / smoothing);                                                
         smoothVector.y = Mathf.Lerp (smoothVector.y, md.y, 1f / smoothing);                                                
         camLook.y = Mathf.Clamp(camLook.y, -65f, 65f);                                                                    
         camLook += smoothVector;                                                                                        
 
         transform.localRotation = Quaternion.AngleAxis (-camLook.y, Vector3.right);                                        
         characterCapsule.transform.localRotation = Quaternion.AngleAxis (camLook.x, characterCapsule.transform.up);        
     }
 }
 
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
0
Best Answer

Answer by Bunny83 · Jun 02, 2018 at 10:31 PM

I guess you mean the gameview, not the sceneview? The sceneview is just the editor view. When you start your game Unity switches to the gameview.


Anyways you do not initialize your "camLook" vector so it defaults to (0, 0). If you want to apply the current rotation at start try something like this inside Start:

 camLook.y = transform.localEulerAngles.x;
 camLook.x = characterCapsule.transform.localEulerAngles.y;


Apart from this your "scaling" in line 23 could be simplified to

 md *= sensitivity * smoothing;

Even when you want seperate factors for x and y this would be way better:

 md.x *= sensitivity * smoothing;
 md.y *= sensitivity * smoothing;


Finally you should swap those two lines (#27 and #28):

 camLook.y = Mathf.Clamp(camLook.y, -65f, 65f);                                                                    
 camLook += smoothVector;                                                                                        

Add the change first and do the clamping after the change. Otherwise you get some jittering at the clamplimit.


ps: I don't think the last line (#31) is correct. You apply a local rotation but the rotation is around a worldspace axis. Maybe in your case it works but this generally seem to be wrong. It seems you just want to rotate around Vector3.up but it's hard to tell without knowing the exact setup of the gameobjects

Comment
Add comment · Show 4 · 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 Ledifen · Jun 02, 2018 at 11:09 PM 0
Share

@Bunny83 Thank you for the quick reply :) I've changed the lines you pointed out for optimisation, thanks :). (I did mean the scene view, with the editor and everything. I choose a position for my player there, and when I hit play, the position is reset).

Unfortunately, the second line to apply the current rotation returns an error ("... Unity engine.tranform does not contain a definition for y..."

I made this script following a tutorial, and while I understand the general idea of what it does, I do not have the capacity to explain really well the last line. All I can say is that it works. But thanks for pointing that out, it's always good for my knowledge to know that :)

avatar image Bunny83 Ledifen · Jun 03, 2018 at 12:48 AM 0
Share

Yes it was a typo, ^^ fixed it in my answer

avatar image Ledifen Bunny83 · Jun 03, 2018 at 08:17 AM 0
Share

Ok so, I tested it and the rotation of the capsule works just perfectly. :) Thanks! But the camera snaps to -65 for its x rotation while it should be - 22. I suppose it has something to do with the clamp since it's -65 too, and indeed, when I change the clamp value, the camera x rotation snaps to the new clamp value. I think understand l what the line camLook.y = transform.localEulerAngles.x; does but I dont see why it would snap the the clamp value since -22 is within the range. I've tried many x value (within or outside the clamp range) and it turns out it's completely random (at least it is for me). The only thing I know is when I put a negative value whithin the range, it snaps to -65.

Show more comments

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

118 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

Related Questions

Rotation resetted on start 1 Answer

How to rotate FPSController in mobile? 1 Answer

When I hit play the character rotates 2 Answers

Minor scale issue on play mode 0 Answers

horizontal recoil reset with slerp 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