I have a script set up for general fps movement and wall-running. I want to tilt the camera on the z-axis accordingly when the player is a wall running but if I don’t that it overrides the y-axis camera rotation
Here I will attach the script for the movement of the player which has the wall running. The four lines found at lines 102, 103, 108, and 109 are the way I tried rotating the camera and all of them override the y-axis movement of the camera.
using System.Collections;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
using Unity.VisualScripting;
using UnityEditor.Experimental.GraphView;
using UnityEditor.XR;
using UnityEngine;
public class PlayerMove : MonoBehaviour
{
public CharacterController cc;
public Rigidbody rb;
public GameObject Camera;
public Transform Orientation;
public float Speed;
public float gravity;
public float Jumph;
public int Jump=1;
public bool Gravity;
public Transform GroundCheck;
public float groundDist;
public LayerMask GroundMask;
Vector3 velocity;
public bool Grounded;
//Wall running
public LayerMask Wallmsc;
public float WRforce, WRmaxTime;
public bool WallLeft, WallRight;
public bool WR;
public float WRcamTilt;
void Update()
{
Movement();
CheckForWall();
WRInput();
}
void Movement()
{
Grounded = Physics.CheckSphere(GroundCheck.position, groundDist);
if (Grounded && velocity.y < 0)
{
velocity.y = -2f;
}
float X = Input.GetAxis("Horizontal");
float Z = Input.GetAxis("Vertical");
Vector3 move = transform.right * X + transform.forward * Z;
cc.Move(move * Speed * Time.deltaTime);
if (Input.GetButtonDown("Jump") && Jump > 0)
{
Jump = Jump - 1;
velocity.y = 0;
velocity.y = Mathf.Sqrt(Jumph * 2f * gravity);
if (WR == true)
{
WRend();
Jump = Jump - 1;
velocity.y = 0;
velocity.y = Mathf.Sqrt(Jumph * 2f * gravity);
}
}
if (Grounded == true)
{
Jump = 1;
Speed = 10;
}
if (Gravity == true)
{
velocity.y += -gravity * Time.deltaTime;
cc.Move(velocity * Time.deltaTime);
}
}
//wallrunning:
private void WRInput()
{
//Start wallrun
if (Input.GetKey(KeyCode.W) && Input.GetKey(KeyCode.D) && WallRight && !Grounded) WRstart();
if (Input.GetKey(KeyCode.W) && Input.GetKey(KeyCode.A) && WallLeft && !Grounded) WRstart();
if(Input.GetKeyUp(KeyCode.W) || WallLeft && Input.GetKeyUp(KeyCode.A) || WallRight && Input.GetKeyUp(KeyCode.D))
{
WRend();
}
}
private void WRstart()
{
Gravity = false;
WR = true;
Speed = 20;
//Character need to stick to wall
if (WallRight)
{
rb.AddForce(Orientation.right * WRforce/5 * Time.deltaTime);
//Camera.transform.localRotation = Quaternion.Euler(0, Camera.transform.localRotation.y, WRcamTilt);
//Camera.transform.localRotation = Quaternion.Euler(0, 0, WRcamTilt);
}
else
{
rb.AddForce(-Orientation.right * WRforce / 5 * Time.deltaTime);
//Camera.transform.localRotation = Quaternion.Euler(0, Camera.transform.localRotation.y, -WRcamTilt);
//Camera.transform.localRotation = Quaternion.Euler(0, 0, -WRcamTilt);
}
Invoke("WRend", WRmaxTime);
}
private void WRend()
{
Gravity = true;
WR = false;
}
private void CheckForWall()
{
WallRight = Physics.Raycast(transform.position, Orientation.right, 1f, Wallmsc);
WallLeft = Physics.Raycast(transform.position, -Orientation.right, 1f, Wallmsc);
//leave wall run
if (!WallRight && !WallLeft) WRend();
if (WallRight || WallLeft)
{
Jump = 2;
}
}
}
How do I tilt this camera while still allowing the y-axis mouse movement of the camera script?
Additionally: here is the camera fps look script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerCameraLook : MonoBehaviour
{
[SerializeField]
Transform character;
public float sensitivity = 2;
public float smoothing = 1.5f;
Vector2 velocity;
Vector2 frameVelocity;
void Start()
{
Cursor.lockState = CursorLockMode.Locked;
}
private void Update()
{
// Get smooth velocity.
Vector2 mouseDelta = new Vector2(Input.GetAxisRaw("Mouse X"), Input.GetAxisRaw("Mouse Y"));
Vector2 rawFrameVelocity = Vector2.Scale(mouseDelta, Vector2.one * sensitivity);
frameVelocity = Vector2.Lerp(frameVelocity, rawFrameVelocity, 1 / smoothing);
velocity += frameVelocity;
velocity.y = Mathf.Clamp(velocity.y, -90, 90);
// Rotate camera up-down and controller left-right from velocity.
transform.localRotation = Quaternion.AngleAxis(-velocity.y, new Vector3(1,transform.localRotation.y,transform.localRotation.z));
character.localRotation = Quaternion.AngleAxis(velocity.x, new Vector3(character.localRotation.x,1,character.localRotation.z));
}
}