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
0
Question by maellepr · Oct 03, 2020 at 10:03 AM · rotationcamera rotatereturnkey pressedinitial

How to make the camera return to unitial position when no button are pushed

Hello i'm looking for a way to make the camera rotation back to the initial position when no button are pushed on the joystick. Right now my code is :

using System.Collections; using System.Collections.Generic; using UnityEngine;

public class MouseLook : MonoBehaviour

{

 public float mouseSensitivity = 100f;
 public Transform playerBody;
 float xRotation = 0f;


 // Start is called before the first frame update
 void Start()
 {
     Cursor.lockState = CursorLockMode.Locked;
 }

 // Update is called once per frame
 void Update()
 {
     
 float mouseX = Input.GetAxis("Horizontal") * mouseSensitivity * Time.deltaTime;
 float mouseY = Input.GetAxis("Vertical") * mouseSensitivity * Time.deltaTime;

 xRotation -= mouseY;
 xRotation = Mathf.Clamp(xRotation, -90f, 90);


 transform.localRotation = Quaternion.Euler(xRotation, 0f, 0f);
 playerBody.Rotate(Vector3.up * mouseX);


 }

}

i'm very much a beginner, need some help please !

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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Shrimpey · Oct 03, 2020 at 10:42 AM

Simplest would probably be to do sth like this:

 if(mouseY == 0.0f){
     xRotation = 0.0f;
 }else{
     ...
 }

but that would snap your rotation instantly. I suggest to read about linear interpolation to smooth it out (Mathf.Lerp). With lerping you'd do sth like:

 if(mouseY == 0.0f){
     xRotation = Mathf.Lerp(xRotation, 0.0f, 0.1f);
 }else{
     ...
 }

Where 0.1f is your step value, lower it to make it slower, or vice versa. You can also make it just like your original approach, so lower value towards 0.0f by increasing/decreasing it. But give lerp a try, it makes the transition much smoother.

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 Shrimpey · Oct 03, 2020 at 10:45 AM 0
Share

Also a note: when using const value as a mathf.lerp step, it's better to do it inside FixedUpdate() instead of Update(), because otherwise smoothing effect will behave differently depending on your framerate.

avatar image
0

Answer by maellepr · Oct 03, 2020 at 11:08 AM

Thanks for the answer @Shrimpey ! It doesn't seems to work. I think when i use if(mouseY == 0.0f){ ... the camera go back the its original position only when the camera is already at its original position 0. I think i need a code that say if mouseY doesn't have any acceleration... I don't know if i'm clear

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 Shrimpey · Oct 03, 2020 at 11:28 AM 0
Share

I'm not sure what your input setup is in Input settings, but if you didn't change anything, you should probably use "$$anonymous$$ouse X" and "$$anonymous$$ouse Y" axes instead of "Horizontal" and "Vertical". With those your Input.GetAxis will measure acceleration, not position.

avatar image Shrimpey Shrimpey · Oct 03, 2020 at 11:33 AM 0
Share

Post your code if it's still not working, my guess is that you're still overriding something after checking for mouseY==0.0f.

avatar image maellepr Shrimpey · Oct 03, 2020 at 11:52 AM 0
Share

I change "Horizontal" and "Vertical" to be "$$anonymous$$ouse X" and "$$anonymous$$ouse Y" but it still doesn't work. Here's my code:

using System.Collections; using System.Collections.Generic; using UnityEngine;

public class $$anonymous$$ouseLook : $$anonymous$$onoBehaviour

{

 public float mouseSensitivity = 100f;
 public Transform playerBody;
 float xRotation = 0f;




 // Start is called before the first frame update
 void Start()
 {

     Cursor.lockState = CursorLock$$anonymous$$ode.Locked;
   
 }

 // Update is called once per frame
 void FixedUpdate()
 {

 float mouseX = Input.GetAxis("$$anonymous$$ouse X") * mouseSensitivity * Time.deltaTime;
 float mouseY = Input.GetAxis("$$anonymous$$ouse Y") * mouseSensitivity * Time.deltaTime;

 
 if (mouseY == 0.0f){

  xRotation = $$anonymous$$athf.Lerp(xRotation,0.0f,0.1f); 

 }else{

 xRotation -= mouseY;
 xRotation = $$anonymous$$athf.Clamp(xRotation, -90f, 90);


 transform.localRotation = Quaternion.Euler(xRotation, 0f, 0f);
 playerBody.Rotate(Vector3.up * mouseX);

}

 }
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

182 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 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 along the y-axis with damping not working! 0 Answers

Camera follow car (Y rotation identity) 2 Answers

Return camera to start position smoothly 2 Answers

Swapping VR axis of rotation 0 Answers

Third person follow camera on a sphere 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