I’m making a game where you walk with your WASD and look around with YGHJ or ZGHJ if you’re on QWERTZ keyboard. I’ve already done the walking but do you know how I could look around with yghj.
I’ve already tried but when I ain’t presing yghj, the camera still moves.
Here’s my existing code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CameraController : MonoBehaviour
{
public GameObject player_mesh;
public Transform player_controller;
private float mouse_sensitivity = 100f;
private float xRotation = 0f;
private float mouseY;
private float mouseX;
private bool look_z = false;
private bool look_h = false;
private bool look_j = false;
private bool look_g = false;
// Start is called before the first frame update
void Start()
{
Cursor.visible = false;
Cursor.lockState = CursorLockMode.Locked;
}
// Update is called once per frame
void Update()
{
if(Input.GetKey(“z”))
{
mouseY = 1 * mouse_sensitivity * Time.deltaTime;
look_z = true;
}
if(Input.GetKey(“h”))
{
mouseY = -1 * mouse_sensitivity * Time.deltaTime;
look_h = true;
}
if(Input.GetKey(“j”))
{
mouseX = 1 * mouse_sensitivity * Time.deltaTime;
look_j = true;
}
if(Input.GetKey(“g”))
{
mouseX = -1 * mouse_sensitivity * Time.deltaTime;
look_g = true;
}
if (look_z == false && look_h == false)
{
mouseY = 0;
}
if (look_j == false && look_g == false)
{
mouseX = 0;
}
xRotation -= mouseY;
xRotation = Mathf.Clamp(xRotation, -90f, 90f);
transform.localRotation = Quaternion.Euler(xRotation, 0f, 0f);
player_controller.Rotate(Vector3.up * mouseX);
}
}