Ранее я пробовал использовать общедоступный игровой объект и вращать его, но камера вообще не вращался, поэтому я попробовал localEulerAngles, поскольку использовал его ранее в сценарии.
Код: Выделить всё
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class FirstPersonCamera : MonoBehaviour
{
// Variables
public Transform player;
public float mouseSensitivity = 2f;
float cameraVerticalRotation = 0f;
public float recoilAmount;
public float recoilRecoverySpeed;
float recoverToAngle;
float ammo = 12;
bool lockedCursor = true;
void Start()
{
// Lock and Hide the Cursor
Cursor.visible = false;
Cursor.lockState = CursorLockMode.Locked;
}
void Update()
{
// Collect Mouse Input
float inputX = Input.GetAxis("Mouse X")*mouseSensitivity;
float inputY = Input.GetAxis("Mouse Y")*mouseSensitivity;
// Rotate the Camera around its local X axis
cameraVerticalRotation -= inputY;
cameraVerticalRotation = Mathf.Clamp(cameraVerticalRotation, -90f, 90f);
transform.localEulerAngles = Vector3.right * cameraVerticalRotation;
// Rotate the Player Object and the Camera around its Y axis
player.Rotate(Vector3.up * inputX);
//detect shooting
if(Input.GetKeyDown(KeyCode.Mouse0))
{
ammo--;
addRecoil();
}
//detect reload
if(Input.GetKeyDown(KeyCode.R))
{
ammo = 12;
}
}
void addRecoil()
{
//set return angle
recoverToAngle = transform.localEulerAngles.y;
transform.localEulerAngles = new Vector3(0f, recoilAmount, 0f);
//recover from recoil to orig position upon firing
recoilRecover();
}
void recoilRecover()
{
while(transform.localEulerAngles.y > recoverToAngle)
{
transform.localEulerAngles = new Vector3(0f, -recoilRecoverySpeed, 0f);
}
}
}
Подробнее здесь: https://stackoverflow.com/questions/793 ... left-click
Мобильная версия