Ich habe es schon einmal versucht, ein öffentliches Spielobjekt zu verwenden und es zu drehen, aber die Kamera würde sich überhaupt nicht drehen, also habe ich localEulerAngles ausprobiert, da ich es früher im Skript verwendet habe.
Code: Select all
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);
}
}
}