- Home /
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 !
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.
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.
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
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.
Post your code if it's still not working, my guess is that you're still overriding something after checking for mouseY==0.0f.
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);
}
}
Your answer
Follow this Question
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