При нажатии wasd персонаж идет только вперед
Делал ходьбу от 3-го лица с гайда на ютубчике и столкнулся с проблемой, когда я нажимаю любую из кнопок wasd или стрелок движения, мой персонаж идет вперед. Я перепроверял код 100 раз, вроде все также как на видео, но у него работает, а у меня нет. Я предполагаю, что это как то связано с тем, что движение происходит за счет анимации. Помимо этого при повороте мышью, поворачивается сам персонаж, а не камера вокруг него.
Скрипт передвижения игрока:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CharacterMovement : MonoBehaviour
{
public Transform cameraTransform;
public CharacterConfig characterConfig;
public Animator anim;
public float vertical;
public float horizontal;
public float moveAmount;
public float dirSpeed;
public Vector3 rotationDirection;
public Vector3 moveDirection;
public void MoveUpdate()
{
vertical = Input.GetAxis("Vertical");
horizontal = Input.GetAxis("Horizontal");
moveAmount = Mathf.Clamp01(Mathf.Abs(vertical) + Mathf.Abs(horizontal));
anim.SetFloat("vertical", moveAmount, 0.15f, Time.deltaTime);
Vector3 moveDir = cameraTransform.forward * vertical;
moveDir += cameraTransform.right * horizontal;
moveDir.Normalize();
moveDirection = moveDir;
rotationDirection = cameraTransform.forward;
RotationNormal();
}
public void RotationNormal()
{
Vector3 targetDir = rotationDirection;
targetDir.y = 0;
if (targetDir == Vector3.zero)
targetDir = transform.forward;
Quaternion lookDir = Quaternion.LookRotation(targetDir, Vector3.up);
Quaternion targetRot = Quaternion.Slerp(transform.rotation, lookDir, dirSpeed);
transform.rotation = targetRot;
}
}
скрипт вращения камеры:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CameraHandler : MonoBehaviour
{
public Transform camTrans;
public Transform Character;
public Transform mTransform;
public CharacterConfig characterConfig;
public CameraConfig cameraConfig;
public float delta;
public float mouseX;
public float mouseY;
public float smoothX;
public float smoothY;
public float smoothXvelocity;
public float smoothYvelocity;
public float lookAngel;
public float titlAngel;
private void Update()
{
FixedTick();
}
void FixedTick()
{
delta = Time.deltaTime;
HundlePosition();
HundleRotation();
Vector3 targetPosition = Vector3.Lerp(mTransform.position, Character.position, 1);
mTransform.position = targetPosition;
}
void HundlePosition()
{
float targetX = cameraConfig.normalX;
float targetY = cameraConfig.normalY;
float targetZ = cameraConfig.normalZ;
}
void HundleRotation()
{
mouseX = Input.GetAxis("Mouse X");
mouseY = Input.GetAxis("Mouse Y");
if (cameraConfig.turnSmooth > 0)
{
smoothX = Mathf.SmoothDamp(smoothX, mouseX, ref smoothXvelocity, cameraConfig.turnSmooth);
smoothY = Mathf.SmoothDamp(smoothY, mouseY, ref smoothYvelocity, cameraConfig.turnSmooth);
}
else
{
smoothX = mouseX;
smoothY = mouseY;
}
lookAngel += smoothX * cameraConfig.x_rot_speed;
Quaternion targetRot = Quaternion.Euler(0, lookAngel, 0);
mTransform.rotation = targetRot;
titlAngel -= smoothY * cameraConfig.y_rot_speed;
titlAngel = Mathf.Clamp(titlAngel, cameraConfig.minAngel, cameraConfig.maxAngel);
}
}