Tilt camera on movement
Asked Answered
M

4

0

i have a problem. i want that if i move left or right, the camera slightly tilts left and right (on z axis).

now this works perfectly fine if i dont use my mouseLook but somehow together they wont work cause mouselook rotations are somehow overwriting my tilting. can someone give me a hint to how to pull this off? thx in advance

void Start()
{
    initialRot = transform.localRotation;
    // lock cursor
    Cursor.lockState = CursorLockMode.Locked;
}
  
void Update()
{
    CameraRot();
  
    // get mouse axis
    float mouseX = Input.GetAxis("Mouse X") * mouseSensitivity;
    float mouseY = Input.GetAxis("Mouse Y") * mouseSensitivity;
  
    // rotate player around y axis
    player.Rotate(Vector3.up * mouseX);
  
    // clamp cam rotation
    xRotation -= mouseY;
    xRotation = Mathf.Clamp(xRotation, topMaxRot, bottomMaxRot);
  
    // rotate camera around x axis
    transform.localRotation = Quaternion.Euler(xRotation, 0f, 0f);
}
  
void CameraRot()
{
    float rotZ = -Input.GetAxis("Horizontal") * rotAmount;
  
    Quaternion finalRot = Quaternion.Euler(0, 0, rotZ);
    transform.localRotation = Quaternion.Lerp(initialRot, finalRot, smoothRotAmount);
}
Mapp answered 19/1, 2024 at 19:5 Comment(0)
M
0

I actually just figured it out. I pretty much clamped the z axis rotation in this line

transform.localRotation = Quaternion.Euler(xRotation, 0f, 0f);

so in case anyone wondering I just made that the z axis is not 0 rather than just the rotation of the current z axis. That’s weird to explain but here is the fixed line

Vector3 v = transform.rotation.eulerAngles;
transform.localRotation = Quaternion.Euler(xRotation, 0, v.z);
Mapp answered 24/1, 2024 at 0:54 Comment(2)

It worked for me. Thanks a million! Do change the line that says Quaternion finalRot = Quaternion.Euler(0, 0, rotZ); to Quaternion finalRot = Quaternion.Euler(xRotation, 0, rotZ); or else you cannot look up.

Coalesce

YOU ARE A LIFE SAVER!!!! I've been looking EVERYWHERE for a solution! thank you so much!!

Peaked
T
0

This usually has to do with at what point do you call the move part of the script and the camera position part of the script. I would take a look at your script to make sure that the camera looks at, or sits at the right point both when its moving and when its stationary.

Tittup answered 16/2, 2020 at 7:33 Comment(0)
E
0

@Mapp

Change a bit your post. It doesnt work.

I found this to be working well with this setup:
[198457-снимок.png|198457]

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
  
public class PlayerCam : MonoBehaviour
{
  
    public float sensativityX, sensativityY;
    public Transform player;
    public Transform cameraPosition;
    public Transform orientation;
  
    float xRotation;
    float yRotation;
  
    public float _tiltAmount = 5;
    public float _rotationSpeed = 0.5f;
  
    void Start()
    {
        Cursor.lockState = CursorLockMode.Locked;
        Cursor.visible = false;
    }
  
    // Update is called once per frame
    void Update()
    {
        Tilt();
        transform.position = cameraPosition.position;
  
        float mouseX = Input.GetAxisRaw("Mouse X") * Time.deltaTime * sensativityX;
        float mouseY = Input.GetAxisRaw("Mouse Y") * Time.deltaTime * sensativityY;
  
       
        xRotation -= mouseY;
        xRotation = Mathf.Clamp(xRotation, -90f, 90f);
  
        yRotation += mouseX;
  
        Vector3 v = transform.rotation.eulerAngles;
        transform.localRotation = Quaternion.Euler(xRotation, yRotation, v.z);
        orientation.rotation = Quaternion.Euler(0, yRotation, 0);
       
    }
    
    public void Tilt()
    {
        float rotZ = -Input.GetAxis("Horizontal") * _tiltAmount;
  
        Quaternion finalRot = Quaternion.Euler(xRotation, yRotation, rotZ);
        transform.localRotation = Quaternion.RotateTowards(transform.localRotation, finalRot, _rotationSpeed);
    }
  
   
  
}
Empson answered 19/1, 2024 at 19:7 Comment(1)

when i strafe left to right it doesnt work well doesnt smooth between left and right any fixes?

Whiteley
B
0

it works yes, but i found that i get more leaning with lower frame rate, i.e if i get 15 to 20 degrees while rotating at 60fps, i get 5 to 8 degrees at 450fps along with stuttering as if it tries to get back to a value of 0.

Brindled answered 19/1, 2024 at 18:30 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.