Код: Выделить всё
using System;
using UnityEngine;
using UnityEngine.InputSystem;
public class PlayerControl : MonoBehaviour
{
[SerializeField] Camera p_camera;
[SerializeField] InputActionAsset p_inputMap;
//Input//
InputAction m_lookAction;
InputAction m_moveAction;
InputAction m_jumpAction;
Vector2 moveActions;
Vector2 lookActions;
private Rigidbody rb;
private float xRotation;
private float yRotation;
[SerializeField] float sensitivity;
[SerializeField] float maxSpeed;
[SerializeField] float airAccel;
[SerializeField] float groundAccel;
[SerializeField] float deAccel;
[SerializeField] float jumpForce;
[SerializeField] bool isGround;
private void Awake()
{
rb = GetComponent();
//İmleç ayarları.
Cursor.lockState = CursorLockMode.Locked;
Cursor.visible = false;
//Input maping
m_lookAction = p_inputMap.FindAction("Player/Look");
m_moveAction = p_inputMap.FindAction("Player/Move");
m_jumpAction = p_inputMap.FindAction("Player/Jump");
m_lookAction.Enable();
m_moveAction.Enable();
m_jumpAction.Enable();
}
// Her framede çalışır
void Update()
{
Look();
}
//Her fizik saniyesindi çalışıyor
private void FixedUpdate()
{
Move();
Jump();
}
//Gorund check yapıldığı yer
private void OnTriggerStay(Collider other)
{
isGround = true;
}
private void OnTriggerExit(Collider other)
{
isGround = false;
}
void Look()
{
//Fare inputlarını güncellemem
lookActions = m_lookAction.ReadValue();
//Mouse delta değerleri okuma
float mouseX = lookActions.x * sensitivity * Time.deltaTime;
float mouseY = lookActions.y * sensitivity * Time.deltaTime;
//Mouse yukarı-aşağı
xRotation -= mouseY;
xRotation = Mathf.Clamp(xRotation, -89f, 89f);
//Mouse sağa-sola
yRotation = mouseX;
//Kamerayı çevirme yukarı-aşağı
p_camera.transform.localRotation = Quaternion.Euler(xRotation, 0, 0);
//Karakteri sağa-sola çevirme
transform.Rotate(Vector3.up * mouseX);
}
void Move()
{
// TODO: Bunnyhop,acseleriti,airacseleriti ve deaceleriti ekle.
moveActions = m_moveAction.ReadValue();
float currentAccel = isGround ? groundAccel : airAccel;
Vector3 moveDir = transform.forward * moveActions.y + transform.right * moveActions.x;
Vector3 targetVelocity = moveDir * maxSpeed;
Vector3 currentHorizontalVelocity = new(rb.linearVelocity.x, 0, rb.linearVelocity.z);
Vector3 newHorizontalVelocity = Vector3.MoveTowards(currentHorizontalVelocity, targetVelocity, currentAccel);
Vector3 newHorizontalDeAccel = Vector3.MoveTowards(currentHorizontalVelocity, targetVelocity, deAccel);
float walkSpeed = newHorizontalVelocity.magnitude;
Debug.Log(walkSpeed);
if (moveActions.magnitude > 0)
{
rb.linearVelocity = new Vector3(newHorizontalVelocity.x, rb.linearVelocity.y, newHorizontalVelocity.z);
}
else
{
rb.linearVelocity = new Vector3(newHorizontalDeAccel.x, rb.linearVelocity.y, newHorizontalDeAccel.z);
}
}
void Jump()
{
if (isGround)
{
if (m_jumpAction.IsPressed())
{
rb.linearVelocity = new Vector3(rb.linearVelocity.x, jumpForce, rb.linearVelocity.z);
}
}
}
}
Я пробовал:
Если я зафиксирую камеру на оси Y в Unity, камера будет двигаться так, как если бы она была прикреплена, и похоже, что у нее падает FPS.
Подробнее здесь: https://stackoverflow.com/questions/798 ... axis-issue