Код: Выделить всё
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class playerMovement : MonoBehaviour
{
public CharacterController controller;
public float speed = 0f;
public float gravity = -9.81f * 2;
public float jumpHeight = 3f;
public Transform groundCheck;
public float groundDistance = 0.5f;
public LayerMask groundMask;
Vector3 velocity;
public bool isGrounded;
void Update()
{
isGrounded = Physics.CheckSphere(groundCheck.position, groundDistance, groundMask);
if (isGrounded && velocity.y < 0)
{
velocity.y = -2f;
}
float x = Input.GetAxis("Horizontal");
float z = Input.GetAxis("Vertical");
Vector3 move = transform.right * x + transform.forward * z;
controller.Move(move * speed * Time.deltaTime);
if (Input.GetButtonDown("Jump") && isGrounded)
{
velocity.y = Mathf.Sqrt(jumpHeight * -2f * gravity);
}
velocity.y += gravity * Time.deltaTime;
controller.Move(velocity * Time.deltaTime);
}
}
Код: Выделить всё
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class mouseMovement : MonoBehaviour
{
public float mouseSensitivity = 500f;
float xRotation = 0f;
float yRotation = 0f;
public float groundDistance = 0.8f;
public Transform groundCheck;
Vector3 initialGroundCheckLocalPosition;
Vector3 groundCheckOffset;
void Start()
{
Cursor.lockState = CursorLockMode.Locked;
groundCheckOffset = groundCheck.position - transform.position;
}
void Update()
{
float mouseX = Input.GetAxis("Mouse X") * mouseSensitivity * Time.deltaTime;
float mouseY = Input.GetAxis("Mouse Y") * mouseSensitivity * Time.deltaTime;
// yukarı aşağı bakma mekaniği
xRotation -= mouseY;
// bakış açısının dönüş sınırını belirlemek için (sonsuza kadar yukarı veya aşağı bakmamak için)
xRotation = Mathf.Clamp(xRotation, -45f, 45f);
// sağa sola bakma mekaniği
yRotation += mouseX;
transform.localRotation = Quaternion.Euler(xRotation, yRotation, 0f);
AdjustGroundCheckPosition();
}
void AdjustGroundCheckPosition()
{
Vector3 adjustedPosition = transform.position + transform.rotation * initialGroundCheckLocalPosition;
groundCheck.position = adjustedPosition;
}
}
Подробнее здесь: https://stackoverflow.com/questions/786 ... controller
Мобильная версия