I am new to unity and want to make an FPS game. I want the gun to be synced to the camera like in most FPS games where if you look up, the gun moves up with you. I tried using some code(attached to the camera):
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class MouseLook : MonoBehaviour
{
public float mouseSensitivity = 100f;
public Transform playerBody;
public Transfomr playerWeapon;
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("Mouse X") * mouseSensitivity * Time.deltaTime;
float mouseY = Input.GetAxis("Mouse Y") * mouseSensitivity * Time.deltaTime;
xRotation -= mouseY;
xRotation = Mathf.Clamp(xRotation, -90f, 90f);
transform.localRotation = Quaternion.Euler(xRotation, 0f, 0f);
playerBody.Rotate(Vector3.up * mouseX);
playerWeapon.Rotate(Vector3.left * mouseY);
}
}
but all that did was rotate the gun in sync with the camera, not move it. If anyone is willing to help fix this or explain how it works than I would be very grateful!