Hi everyone, I’m learning how to make a character movement, I’m trying to make a platformer with 3D assets, so the character is supposed to only move in the X AXIS, which it does now, it moves in +X or -X, the problem is that the character is always facing forward +X, so with some documentation and a Unity learning tutorial, I’m trying to make my character turn in the direction it’s moving which can be -X or +X, the problem is that I can’t get it to work, I know there’s some problem in the rotation parameter but I don’t know why, I barely understand Vector3.RotateTowards or Quaternion.LookRotation, I try to fix it by myself but it’s not working, so I’m here for some help, the problem is when I include these 3 lines which are the lines that are supposed to make my character turn to face -x or +x, first, now the character at first is facing Z, and when I move to X or -X, then the character just turns to face -y, meaning my character just sleeps on the ground facing -y, but still moves only to -x and +x, i don’t know what else to do
the 3 code lines:
Vector3 desiredForward = Vector3.RotateTowards (transform.forward, Movement, turnSpeed * Time.deltaTime, 0f);
Quaternion Rotation = Quaternion.LookRotation(desiredForward);
SB_Rigidbody.MoveRotation(Rotation);
full code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class SoldierBoyController : MonoBehaviour
{
float speed = 5f;
float turnSpeed = 20f;
Animator SB_Animator;
Rigidbody SB_Rigidbody;
void Start()
{
SB_Animator = GetComponent<Animator>();
SB_Rigidbody = GetComponent<Rigidbody>();
}
void FixedUpdate()
{
float horizontal = Input.GetAxis("Horizontal");
Vector3 Movement = new Vector3 (horizontal, 0f, 0f);
Movement.Normalize ();
bool hasHorizontalInput = !Mathf.Approximately (horizontal, 0f);
if( hasHorizontalInput == true)
{
SB_Animator.SetBool("IsRunning", true);
}
else if (hasHorizontalInput == false)
{
SB_Animator.SetBool("IsRunning", false);
}
Vector3 desiredForward = Vector3.RotateTowards (transform.forward, Movement, turnSpeed * Time.deltaTime, 0f);
Quaternion Rotation = Quaternion.LookRotation(desiredForward);
SB_Rigidbody.MovePosition(SB_Rigidbody.position + Movement * speed * Time.deltaTime);
SB_Rigidbody.MoveRotation(Rotation);
}