Im trying to make the camera movement in Unity but I have a problem, the player will always start with a rotation 0,0,0, even I rotate it in Y before playing.
I the code I rotate the player in Y axis and the camera in X, so the player wont rotate when looking up-down.
Here is the code:
public float cameraSpeed;
//public float smooth = 2;
[Range(0f, 1f)] public float smooth = 0.5f;
Vector2 mouseLook;
Vector2 smoothV;
private Transform cameraTransform;
void Start()
{
//Inicializamos componentes
rb = GetComponent<Rigidbody>();
cameraTransform = Camera.main.transform;
mouseLook.y = Mathf.Clamp(-cameraTransform.localRotation.x, -75, 50);
mouseLook.x = transform.localRotation.y;
}
void CameraMovement()
{
//Declaramos un vector con la direccion del raton
Vector2 md = new Vector2(InputManager.main.HorizontalMouse(), InputManager.main.VerticalMouse());
md *= cameraSpeed / smooth;
smoothV = Vector2.Lerp(smoothV, md, smooth);
mouseLook += smoothV;
//Limitamos el angulo de la camara para que no de vueltas
mouseLook = new Vector2(mouseLook.x, Mathf.Clamp(mouseLook.y, -75, 50));
//Hacemos que rote la camara en el eje x y el jugador en el eje y
Camera.main.transform.localRotation = Quaternion.AngleAxis(-mouseLook.y, Vector3.right);
transform.localRotation = Quaternion.AngleAxis(mouseLook.x , transform.up);
}