moveDirection.y -= gravity * Time.deltaTime; controller.Move(moveDirection * Time.deltaTime); } } Персонаж вращается вокруг своей оси и как переписать этот скрипт чтоб при повороте головы направление в которое мы смотрим, при нажатии на w, шли не в бок, а туда куда смотрим. Вот скрипт на сглаживание камеры: using UnityEngine;
public class CameraFollow : MonoBehaviour { public Transform target; public Vector3 offset; public float smoothSpeed = 12f;
transform.rotation = Quaternion.Slerp(transform.rotation, target.rotation, smoothSpeed * Time.deltaTime); } } И вот скрипт на управление камерой с помощью мыши: using UnityEngine;
public class MouseLook : MonoBehaviour { public float mouseSensitivity = 100f; public Transform playerBody;
public class Player : MonoBehaviour
{
public float speed = 5.0f;
public float jumpHeight = 2.0f;
private CharacterController controller;
private Vector3 moveDirection = Vector3.zero ;
private float gravity = 9.81f;
void Start()
{
controller = GetComponent<CharacterController>();
}
void Update()
{
if (controller.isGrounded)
{
float moveHorizontal = Input.GetAxis("Horizontal");
float moveVertical = Input.GetAxis("Vertical");
moveDirection = new Vector3(moveHorizontal, 0.0f, moveVertical);
moveDirection *= speed;
if (Input.GetButton("Jump"))
{
moveDirection.y = Mathf.Sqrt(jumpHeight * 2.0f * gravity);
}
}
moveDirection.y -= gravity * Time.deltaTime;
controller.Move(moveDirection * Time.deltaTime);
}
}
Персонаж вращается вокруг своей оси и как переписать этот скрипт чтоб при повороте головы направление в которое мы смотрим, при нажатии на w, шли не в бок, а туда куда смотрим.
Вот скрипт на сглаживание камеры:
using UnityEngine;
public class CameraFollow : MonoBehaviour
{
public Transform target;
public Vector3 offset;
public float smoothSpeed = 12f;
void LateUpdate()
{
Vector3 desiredPosition = target.position + target.forward * offset.z + target.right * offset.x + target.up * offset.y;
transform.position = Vector3.Lerp(transform.position, desiredPosition, smoothSpeed * Time.deltaTime);
transform.rotation = Quaternion.Slerp(transform.rotation, target.rotation, smoothSpeed * Time.deltaTime);
}
}
И вот скрипт на управление камерой с помощью мыши:
using UnityEngine;
public class MouseLook : MonoBehaviour
{
public float mouseSensitivity = 100f;
public Transform playerBody;
float xRotation = 0f;
float yRotation = 0f;
void Start()
{
Cursor.lockState = CursorLockMode.Locked;
}
void Update()
{
float mouseX = Input.GetAxis("Mouse X") * mouseSensitivity * Time.deltaTime;
float mouseY = Input.GetAxis("Mouse Y") * mouseSensitivity * Time.deltaTime;
xRotation -= mouseY;
xRotation = (transform.rotation.eulerAngles.x + mouseY) % 360;
yRotation += mouseX;
yRotation = (transform.rotation.eulerAngles.y + mouseX) % 360;
transform.localRotation = Quaternion.Euler(xRotation, yRotation, 0f);
playerBody.Rotate(Vector3.up * mouseX);
playerBody.Rotate(Vector3.left * mouseY);
}
}