Hi
I am new to Unity and am currently building a game which the main character walks around the game area fighting enemies. I have managed to get him moving and animating correctly (mostly) but I cannot seem to get him to keep facing the direction of travel after he stops moving. I have been looking through forums and google for a few days now and have tried various different ways to code this but it just will not work and I need any help I can get!
Not sure how to correctly add code to this but here goes…
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerController : MonoBehaviour
{
public float speed = 1f;
Vector3 movement;
Animator anim;
Rigidbody playerRigidbody;
void Awake()
{
anim = GetComponent<Animator>();
playerRigidbody = GetComponent<Rigidbody>();
}
void FixedUpdate()
{
float h = Input.GetAxisRaw("Horizontal");
float v = Input.GetAxisRaw("Vertical");
Vector3 movement = new Vector3(h, 0.0f, v);
transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(movement), 0.15F);
transform.Translate(movement * speed * Time.deltaTime, Space.World);
Move(h, v);
Animating(h, v);
}
void Move(float h, float v)
{
movement.Set(h, 0f, v);
movement = movement.normalized * speed * Time.deltaTime;
playerRigidbody.MovePosition(transform.position + movement);
}
void Animating(float h, float v)
{
bool walking = h != 0f || v != 0f;
anim.SetBool("IsWalking", walking);
}
}