So I’m trying to make a third person character movement script which will allow me to face the camera direction and keep facing that direction until I move it in some other direction.
Here’s my script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class movement : MonoBehaviour {
Transform cam;
Animator anim;
Vector3 directionpos;
Vector3 storecamr;
Vector3 storecamf;
float h;
float v;
public float smooth=10.0f;
// Use this for initialization
void Start () {
cam = Camera.main.transform;
anim = GetComponent<Animator> ();
}
//Update is called after a fixed interval
void FixedUpdate(){
h = Input.GetAxis ("Horizontal");
v = Input.GetAxis ("Vertical");
storecamr = cam.right;
storecamf = cam.forward;
storecamf.y = 0;
storecamr.y = 0;
directionpos = (storecamf * v) + (storecamr * h);
transform.rotation = Quaternion.Slerp (transform.rotation, Quaternion.LookRotation(directionpos), smooth);
anim.SetFloat ("InputX", v);
}
What happens is that the character would rotate but it would only walk in the forward direction and revert to its original rotation when the movement keys are released.
How do I modify my code to make it work?