Hello guys,
A pleasant day to you. I need your help. First, forgive me if I can’t explain properly. Anyway, I created a top down character that can move forward, back, left, right. In the script, I also added a strafing movement but for now it can only do the proper animation when the character is facing forward. If the character is facing left, right or back the script would treat it as forward. Is there a way that wherever the character is facing, the strafe movement will follow? Anyway, here’s the video to see what I’m saying and also attached is the script that I use.
Here’s the script
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerMovement : MonoBehaviour
{
private CharacterController controller;
public float speed = 6f;
public float turnSmoothTime = 0.1f;
private Animator animator;
private void Start()
{
controller = GetComponent<CharacterController>();
animator = GetComponentInChildren<Animator>();
}
private void Update()
{
float horizontal = Input.GetAxisRaw("Horizontal");
float vertical = Input.GetAxisRaw("Vertical");
Vector3 direction = new Vector3(horizontal, 0f, vertical).normalized;
animator.SetInteger("motion", 0);
if (direction.magnitude >= 0.1f)
{
float targetAngle = Mathf.Atan2(direction.x, direction.z) * Mathf.Rad2Deg;
controller.Move(direction * speed * Time.deltaTime);
animator.SetInteger("motion", 1);
if (Input.GetButton("Strafe"))
{
animator.SetBool("Strafe", true);
Quaternion newDirection = Quaternion.LookRotation(direction);
animator.SetFloat("H", horizontal);
animator.SetFloat("V", vertical);
}
else
{
animator.SetBool("Strafe", false);
Quaternion newDirection = Quaternion.LookRotation(direction);
transform.rotation = Quaternion.Slerp(transform.rotation, newDirection, Time.deltaTime * turnSmoothTime);
}
}
}
thank you and looking forward to hearing from you and have a nice day all of you