Hey, so I tried to make a movement script where the camera direction should be the players forward direction. That works very well, but when I walk sideways by pressing A or D the character is turning to the right side and walks backwards.
I would like that the player is walking left from camera direction when A is pressed and right when D is pressed, but dont really know what to change…
I used the new inputsystem with a Cinemachine FreeLook cam and just attached a CharacterController component on the gameobject itself. I’m using this code:
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.InputSystem;
using Cinemachine;
[RequireComponent(typeof(CharacterController))]
public class BasicMovementFinal : MonoBehaviour
{
[SerializeField] private float moveSpeed = 10f;
[SerializeField] private float jumpSpeed = 0.5f;
[SerializeField] private float gravity = 2f;
[SerializeField] private float rotationSpeed = 4f;
private bool isJumped = false;
[SerializeField] private InputActionReference movementControl;
[SerializeField] private InputActionReference jumpControl;
[SerializeField] private CinemachineFreeLook freeLookCam;
private CharacterController characterController;
private Transform cameraTransform;
private void Awake()
{
characterController = gameObject.GetComponent<CharacterController>();
cameraTransform = Camera.main.transform;
}
private void OnEnable()
{
movementControl.action.Enable();
jumpControl.action.performed += HandleJump;
jumpControl.action.Enable();
}
private void OnDisable()
{
movementControl.action.Disable();
jumpControl.action.performed -= HandleJump;
jumpControl.action.Disable();
}
private void HandleJump(InputAction.CallbackContext obj)
{
var value = obj.ReadValue<float>();
isJumped = value >= .5f;
}
private Vector3 moveDirection;
private void FixedUpdate()
{
#region Move
Vector2 input = movementControl.action.ReadValue<Vector2>();
Vector3 inputDirection = new Vector3(input.x, 0, input.y);
Vector3 transformDirection;
//Wenn S gedrückt wird läuft der Character in die entgegengesetzte Richtung in die die Cam zeigt
if (inputDirection.z < 0)
{
Vector3 inverseDirection = new Vector3(inputDirection.x, inputDirection.y, inputDirection.z * -1f);
transformDirection = transform.TransformDirection(inverseDirection);
}
else
transformDirection = transform.TransformDirection(inputDirection);
Vector3 flatMovement = moveSpeed * Time.deltaTime * transformDirection;
moveDirection = new Vector3(flatMovement.x, moveDirection.y, flatMovement.z);
//Jump
if (isJumped && characterController.isGrounded)
moveDirection.y = jumpSpeed;
else if (characterController.isGrounded)
moveDirection.y = 0f;
else
moveDirection.y -= gravity * Time.deltaTime;
//Bewege
characterController.Move(moveDirection);
#endregion
#region Rotation
if (input != Vector2.zero)
{
float targetAngle = Mathf.Atan2(input.x, input.y) * Mathf.Rad2Deg + cameraTransform.eulerAngles.y;
Quaternion rotation = Quaternion.Euler(0f, targetAngle, 0f);
transform.rotation = Quaternion.Lerp(transform.rotation, rotation, Time.deltaTime * rotationSpeed);
}
#endregion
}
}