I am creating a 3d side scroller and I need my player to face the direction he is moving (left or right). I do not want him facing up when I jump and I do not want him facing down when I am not moving.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerController : MonoBehaviour
{
public float moveSpeed;
public float jumpForce;
public CharacterController controller;
public float rotateSpeed;
private Vector3 moveDirection;
public float gravityScale;
// Use this for initialization
void Start()
{
controller = GetComponent<CharacterController>();
}
// Update is called once per frame
void Update()
{
moveDirection = new Vector3(Input.GetAxis("Horizontal") * moveSpeed, moveDirection.y,0);
if (controller.isGrounded)
{
moveDirection.y = 0f;
if (Input.GetButtonDown("Jump"))
{
moveDirection.y = jumpForce;
}
}
moveDirection.y = moveDirection.y + (Physics.gravity.y * gravityScale * Time.deltaTime);
controller.Move(moveDirection * Time.deltaTime);
transform.rotation = Quaternion.LookRotation(moveDirection);
I tried this and I also tried to rotate him then stop the rotation when he hits certain Euler angles.