- Home /
Question by
jamesgernuck · May 20 at 07:26 PM ·
movement scriptnew user
Getting my player to move in the direction it is facing.
I am new to unity and I am trying to get my player to move based on transform.forward, but It still moves based on the world space, I can't get anything to work.
Heres the code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.InputSystem;
public class AnimationAndMovementController : MonoBehaviour
{
PlayerInput playerInput;
CharacterController characterController;
Animator animator;
Vector2 currentMovementInput;
Vector3 currentMovement;
Vector3 currentRunMovement;
bool isMovementPressed;
bool isRunPressed;
float rotationFactorPerFrame = 5.0f;
float runMultiplier = 3.0f;
public Transform cam;
void Awake()
{
playerInput = new PlayerInput();
characterController = GetComponent<CharacterController>();
animator = GetComponent<Animator>();
playerInput.CharacterControls.Move.started += onMovementInput;
playerInput.CharacterControls.Move.canceled += onMovementInput;
playerInput.CharacterControls.Move.performed += onMovementInput;
playerInput.CharacterControls.Run.started += onRun;
playerInput.CharacterControls.Run.canceled += onRun;
}
void onRun (InputAction.CallbackContext context)
{
isRunPressed = context.ReadValueAsButton();
}
void onMovementInput (InputAction.CallbackContext context)
{
currentMovementInput = context.ReadValue<Vector2>();
currentMovement.x = currentMovementInput.x;
currentMovement.z = currentMovementInput.y;
currentRunMovement.x = currentMovementInput.x * runMultiplier;
currentRunMovement.z = currentMovementInput.y * runMultiplier;
isMovementPressed = currentMovementInput.x != 0 || currentMovementInput.y != 0;
}
void handleAnimation()
{
bool isWalking = animator.GetBool("isWalking");
bool isRunning = animator.GetBool("isRunning");
if (isMovementPressed && !isWalking) {
animator.SetBool("isWalking", true);
}
else if (!isMovementPressed && isWalking) {
animator.SetBool("isWalking", false);
}
if ((isMovementPressed && isRunPressed) && !isRunning)
{
animator.SetBool("isRunning", true);
}
else if ((!isMovementPressed || !isRunPressed) && isRunning) {
animator.SetBool("isRunning", false);
}
}
void handleGravity()
{
if (characterController.isGrounded) {
float groundedGravity = -.05f;
currentMovement.y = groundedGravity;
currentRunMovement.y = groundedGravity;
} else {
float gravity = -9.8f;
currentMovement.y += gravity * Time.deltaTime * 2.0f;
currentRunMovement.y += gravity * Time.deltaTime * 2.0f;
}
}
// Update is called once per frame
void Update()
{
handleAnimation();
handleGravity();
if (isRunPressed) {
characterController.Move(currentRunMovement * Time.deltaTime);
} else {
characterController.Move(currentMovement * Time.deltaTime);
}
}
void OnEnable()
{
playerInput.CharacterControls.Enable();
}
void OnDisable()
{
playerInput.CharacterControls.Disable();
}
}
Comment
Your answer

Follow this Question
Related Questions
My player passes through walls 5 Answers
How can I straighten my character's movement and actions? 0 Answers
2D Platformer physics rotation Movement script - How to roll/flip a square on his edges ? 0 Answers
How can I make a unit fire in one direction while still moving in another? 1 Answer
Interrupt a MoveTowards when triggering 2 Answers