Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice

Question My character doesn't receive any input or it just won't move

Discussion in 'Input System' started by frigobar231, Jul 17, 2022.

  1. frigobar231

    frigobar231

    Joined:
    Jul 26, 2021
    Posts:
    1
    I'm new on unity and I'm trying to script the character movement, no error in unity console neither does visual studio say anything, maybe i'm missing something on the key assignment or something i don't know, theres two scripts one for movement and the other for input maybe the last one is the problem help please.

    MOVEMENT:

    namespace Fortesque
    {
    public class playerLocomotion : MonoBehaviour
    {
    Transform cameraObject;
    inputHandler inputHandler;
    Vector3 moveDirection;


    [HideInInspector]
    public Transform myTransform;
    [HideInInspector]
    public AnimatorHandler animatorHandler;

    public new Rigidbody rigidbody;
    public GameObject normalCamera;

    [Header("Stats")]
    [SerializeField]
    float movementSpeed = 5;
    [SerializeField]
    float rotationSpeed = 10;

    void Start()
    {
    rigidbody = GetComponent<Rigidbody>();
    inputHandler = GetComponent<inputHandler>();
    AnimatorHandler = GetComponentInChildren<AnimatorHandler>();
    cameraObject = Camera.main.transform;
    myTransform = transform;
    AnimatorHandler.Initialize();
    }

    public void update()
    {
    float delta = Time.deltaTime;

    inputHandler.TickInput(delta);

    moveDirection = cameraObject.forward * inputHandler.vertical;
    moveDirection += cameraObject.right * inputHandler.horizontal;
    moveDirection.Normalize();


    float speed = movementSpeed;
    moveDirection *= speed;

    Vector3 projectedVelocity = Vector3.ProjectOnPlane(moveDirection, normalVector);
    rigidbody.velocity = projectedVelocity;

    if (AnimatorHandler.canRotate)
    {
    HandleRotation(delta);
    }


    }



    #region Movement
    Vector3 normalVector;
    Vector3 targetPosition;

    public AnimatorHandler AnimatorHandler { get => animatorHandler; set => animatorHandler = value; }

    private void HandleRotation(float delta)
    {
    Vector3 targetDir = Vector3.zero;
    float moveOverride = inputHandler.moveAmount;

    targetDir = cameraObject.forward * inputHandler.vertical;
    targetDir += cameraObject.right * inputHandler.horizontal;

    targetDir.Normalize();
    targetDir.y = 0;

    if (targetDir == Vector3.zero)
    targetDir = myTransform.forward;

    float rs = rotationSpeed;

    Quaternion tr = Quaternion.LookRotation(targetDir);
    Quaternion targetRotation = Quaternion.Slerp(myTransform.rotation, tr, rs * delta);

    myTransform.rotation = targetRotation;
    }



    #endregion
    }

    }



    INPUT:

    namespace Fortesque
    {
    public class inputHandler : MonoBehaviour
    {
    public float horizontal;
    public float vertical;
    public float moveAmount;
    public float mouseX;
    public float mouseY;

    PlayerControls inputActions;

    Vector2 MovementInput;
    Vector2 CameraInput;

    public void OnEnable()
    {
    if (inputActions == null)
    {
    inputActions = new PlayerControls();
    inputActions.PlayerMovement.Movement.performed += inputActions => MovementInput = inputActions.ReadValue<Vector2>();
    inputActions.PlayerMovement.Camera.performed += i => CameraInput = i.ReadValue<Vector2>();
    }

    inputActions.Enable();

    }
    private void onDisable()
    {
    inputActions.Disable();
    }


    public void TickInput(float delta)
    {
    MoveInput(delta);
    }

    private void MoveInput(float delta)
    {
    horizontal = MovementInput.x;
    vertical = MovementInput.y;
    moveAmount = Mathf.Clamp01(Mathf.Abs(horizontal) + Mathf.Abs(vertical));
    mouseX = CameraInput.x;
    mouseY = CameraInput.y;
    }

    }

    }

    P.S.
    I put both the script as components in player object, rigidbody and capsule collider too, they are in the right order i believe, if thats important, in the Imput script i tryed to move with the sliders but nothing happen