Making My Character Face The Direction They Are Moving (3D RPG, Unity Input System)
I have recently cracked being able to get my 3D character to move in a 3D world using a gamepad through Unity's Input System. However, no matter which direction they move, they always face the same direction. I just want them to be able to face the direction they are moving and keep facing the direction they were in when they stop moving. I don't know if the Input System and my current code changes how that is done, so I'm posting the code I have just to be safe. If anyone knows a script that can work for me, please let me know.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.InputSystem;
using UnityEngine.Scripting.APIUpdating;
public class PlayerController : MonoBehaviour
{
PlayerControls controls;
Vector3 Movement;
void Awake()
//Controller input roles
{
controls = new PlayerControls();
controls.Gameplay.Movement.performed += ctx => Movement = ctx.ReadValue<Vector2>();
controls.Gameplay.Movement.canceled += ctx => Movement = Vector3.zero;
}
//Joystick controls
void Update()
{
//Character Movement
Vector3 m = new Vector3(Movement.x, 0, Movement.y) * Time.deltaTime;
transform.Translate(m, Space.World);
}
void OnEnable()
{
controls.Gameplay.Enable();
}
void OnDisable()
{
controls.Gameplay.Disable();
}
}
Answer by jhubbard0221 · Jun 05, 2020 at 06:06 PM
For those following the question, I figured it out. For anyone wanting good third person movement with a gamepad, the YouTube channel Brackeys has two two videos out that you can use to get the results you want.
Gamepad Input Tutorial (Need Input System): https://www.youtube.com/watch?v=p-3S73MaDP8
Third Person Movement (Need Cinemachine): https://www.youtube.com/watch?v=4HpC--2iowE
Following this will allow you to make a character that moves around relative to the camera. They will face the direction they are moving and smoothly transition between directions they are facing. The only issue I have left is I cannot control the camera movement with the right joystick yet, but I have already posted a question about that.
By the end of it I had a movement script that looked like this:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.InputSystem;
public class ThirdPersonMovement : MonoBehaviour
{
PlayerControls controls;
Vector3 Movement;
public CharacterController controller;
public Transform cam;
public float movementSpeed = 6f;
public float turnSmoothTime = 0.1f;
float turnSmoothVelocity;
void Awake()
//Controller input roles
{
controls = new PlayerControls();
controls.Gameplay.Movement.performed += ctx => Movement = ctx.ReadValue<Vector2>();
controls.Gameplay.Movement.canceled += ctx => Movement = Vector3.zero;
}
// Update is called once per frame
void Update()
{
//Character Movement
float horizontal = Input.GetAxisRaw("Horizontal");
float vertical = Input.GetAxisRaw("Vertical");
Vector3 direction = new Vector3(horizontal, 0f, vertical).normalized;
//Character Rotation
if (direction.magnitude >= 0.1f)
{
float targetAngle = Mathf.Atan2(direction.x, direction.z) * Mathf.Rad2Deg + cam.eulerAngles.y;
float angle = Mathf.SmoothDampAngle(transform.eulerAngles.y, targetAngle, ref turnSmoothVelocity, turnSmoothTime);
transform.rotation = Quaternion.Euler(0f, angle, 0f);
Vector3 moveDir = Quaternion.Euler(0f, targetAngle, 0f) * Vector3.forward;
controller.Move(moveDir.normalized * movementSpeed * Time.deltaTime);
}
}
void OnEnable()
{
controls.Gameplay.Enable();
}
void OnDisable()
{
controls.Gameplay.Disable();
}
}
Feel free to copy and paste this, just know that you will need the Input System and the Cinemachine Freelook camera. If anyone has any questions about this, let me know, I might be able to help.
why cant i use PlayerControl?? and also the Unityengine input system
thx
Your answer
Follow this Question
Related Questions
Cinemachine Freelook Camera Movement with Joystick 3 Answers
Character Jump only works randomly and only with the "Y" Button 0 Answers
Making an object move non stop after button input? 1 Answer
Gamepad recognized as joystick and doesn't work 0 Answers
Help! Collision will not work, but all others will 0 Answers