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);
}
}
6 Replies
$details
When you ask a question, make sure you include as much detail as possible. Such as code, the issue you are facing, what you expect the result to be, what .NET version you are using and what platform/environment (if any) are relevant to your question. Upload code here https://paste.mod.gg/, save, and copy the link into chat for others to see your shared code! (see $code for more information on how to paste your code)
A good start would be the actual question you're having
you're way more likely to get helped if you ask a detailed specific question - "i need help to make [vague and potentially complex thing]" with no elaboration isn't something most people will engage with
I don't know what to do, I wanted the camera to work inside that code and like in the games the camera rotates 360 degrees or do I need to make a separate script? for the camera and then attach it to the character on Unity
You might want to head over to the Unity discord