Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question character moves backwards while pressing left/right

Discussion in 'Editor & General Support' started by unity_rLrstvXScQEvow, Jan 27, 2021.

  1. unity_rLrstvXScQEvow

    unity_rLrstvXScQEvow

    Joined:
    Mar 4, 2019
    Posts:
    28
    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:
    Code (CSharp):
    1. using System;
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5. using UnityEngine.InputSystem;
    6. using Cinemachine;
    7.  
    8. [RequireComponent(typeof(CharacterController))]
    9. public class BasicMovementFinal : MonoBehaviour
    10. {
    11.     [SerializeField] private float moveSpeed = 10f;
    12.     [SerializeField] private float jumpSpeed = 0.5f;
    13.     [SerializeField] private float gravity = 2f;
    14.     [SerializeField] private float rotationSpeed = 4f;
    15.     private bool isJumped = false;
    16.  
    17.     [SerializeField] private InputActionReference movementControl;
    18.     [SerializeField] private InputActionReference jumpControl;
    19.     [SerializeField] private CinemachineFreeLook freeLookCam;
    20.  
    21.     private CharacterController characterController;
    22.     private Transform cameraTransform;
    23.  
    24.     private void Awake()
    25.     {
    26.         characterController = gameObject.GetComponent<CharacterController>();
    27.         cameraTransform = Camera.main.transform;
    28.     }
    29.  
    30.     private void OnEnable()
    31.     {
    32.         movementControl.action.Enable();
    33.  
    34.         jumpControl.action.performed += HandleJump;
    35.         jumpControl.action.Enable();
    36.     }
    37.  
    38.     private void OnDisable()
    39.     {
    40.         movementControl.action.Disable();
    41.  
    42.         jumpControl.action.performed -= HandleJump;
    43.         jumpControl.action.Disable();
    44.     }
    45.  
    46.     private void HandleJump(InputAction.CallbackContext obj)
    47.     {
    48.         var value = obj.ReadValue<float>();
    49.         isJumped = value >= .5f;
    50.     }
    51.  
    52.     private Vector3 moveDirection;
    53.  
    54.     private void FixedUpdate()
    55.     {
    56.         #region Move
    57.         Vector2 input = movementControl.action.ReadValue<Vector2>();
    58.         Vector3 inputDirection = new Vector3(input.x, 0, input.y);
    59.         Vector3 transformDirection;
    60.  
    61.         //Wenn S gedrückt wird läuft der Character in die entgegengesetzte Richtung in die die Cam zeigt
    62.         if (inputDirection.z < 0)
    63.         {
    64.             Vector3 inverseDirection = new Vector3(inputDirection.x, inputDirection.y, inputDirection.z * -1f);
    65.             transformDirection = transform.TransformDirection(inverseDirection);
    66.         }
    67.         else
    68.             transformDirection = transform.TransformDirection(inputDirection);
    69.  
    70.         Vector3 flatMovement = moveSpeed * Time.deltaTime * transformDirection;
    71.         moveDirection = new Vector3(flatMovement.x, moveDirection.y, flatMovement.z);
    72.  
    73.         //Jump
    74.         if (isJumped && characterController.isGrounded)
    75.             moveDirection.y = jumpSpeed;
    76.         else if (characterController.isGrounded)
    77.             moveDirection.y = 0f;
    78.         else
    79.             moveDirection.y -= gravity * Time.deltaTime;
    80.  
    81.         //Bewege
    82.         characterController.Move(moveDirection);
    83.  
    84.         #endregion
    85.         #region Rotation
    86.         if (input != Vector2.zero)
    87.         {
    88.             float targetAngle = Mathf.Atan2(input.x, input.y) * Mathf.Rad2Deg + cameraTransform.eulerAngles.y;
    89.             Quaternion rotation = Quaternion.Euler(0f, targetAngle, 0f);
    90.             transform.rotation = Quaternion.Lerp(transform.rotation, rotation, Time.deltaTime * rotationSpeed);
    91.         }
    92.         #endregion
    93.     }
    94. }
    95.  
     
    Nv_Nilong likes this.
  2. Nv_Nilong

    Nv_Nilong

    Joined:
    Jun 7, 2022
    Posts:
    1
    i have the same problem in 2022