esportsman
i need help me C# I need the camera to work at 360 degrees like in shooters
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[RequireComponent(typeof(CharacterController))]
public class PlayerController : MonoBehaviour
{
public float moveSpeed = 5f;
public float lookSpeed = 2f;
public float jumpForce = 8f;
public float gravity = 20f;
private CharacterController controller;
private float verticalRotation = 0f;
private float verticalVelocity = 0f;
private Transform cam;
void Start()
{
controller = GetComponent<CharacterController>();
cam = Camera.main.transform;
Cursor.lockState = CursorLockMode.Locked;
Cursor.visible = false;
}
void Update()
{
// Вращение мышью
float mouseX = Input.GetAxis("Mouse X") * lookSpeed;
float mouseY = Input.GetAxis("Mouse Y") * lookSpeed;
verticalRotation -= mouseY;
verticalRotation = Mathf.Clamp(verticalRotation, -90f, 90f);
cam.localRotation = Quaternion.Euler(verticalRotation, 0f, 0f);
transform.Rotate(Vector3.up * mouseX); // Вращаем игрока (камера внутри)
// Движение
float h = Input.GetAxis("Horizontal");
float v = Input.GetAxis("Vertical");
Vector3 move = transform.right * h + transform.forward * v;
move *= moveSpeed;
// Прыжок и гравитация
if (controller.isGrounded)
{
verticalVelocity = -1f;
if (Input.GetButtonDown("Jump"))
{
verticalVelocity = jumpForce;
}
}
else
{
verticalVelocity -= gravity * Time.deltaTime;
}
move.y = verticalVelocity;
controller.Move(move * Time.deltaTime);
}
}
7 replies