1. Добавить Rigidbody к персонажу
2. Обновить скрипт для работы с Rigidbody
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Mouse : MonoBehaviour
{
public float speed;
public float sensitive;
float horizontal;
float vertical;
float x;
float z;
public GameObject player;
private Animator playerAnimator;
private Rigidbody playerRigidbody;
void Start()
{
Cursor.lockState = CursorLockMode.Locked;
playerAnimator = player.GetComponent<Animator>();
playerRigidbody = player.GetComponent<Rigidbody>();
}
void Update()
{
x = Input.GetAxis("Horizontal") * speed * Time.deltaTime;
z = Input.GetAxis("Vertical") * speed * Time.deltaTime;
horizontal += Input.GetAxis("Mouse X") * sensitive;
vertical += Input.GetAxis("Mouse Y") * sensitive;
vertical = Mathf.Clamp(vertical, -20, 20);
Vector3 newPosition = playerRigidbody.position + player.transform.right * x + player.transform.forward * z;
playerRigidbody.MovePosition(newPosition);
transform.localRotation = Quaternion.Euler(-vertical, 0, 0);
player.transform.localRotation = Quaternion.Euler(0, horizontal, 0);
PlayerController();
}
void PlayerController()
{
if (z > 0)
{
if (Input.GetKey(KeyCode.LeftShift))
{
playerAnimator.SetInteger("Move", 2);
speed = 4;
}
else
{
playerAnimator.SetInteger("Move", 1);
speed = 2;
}
}
else if (z < 0)
{
playerAnimator.SetInteger("Move", -1);
speed = 2;
}
else
{
playerAnimator.SetInteger("Move", 0);
speed = 0;
}
}
}