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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice
  4. Dismiss Notice

My character doesn't move

Discussion in 'Input System' started by Carlitosjft, May 21, 2022.

  1. Carlitosjft

    Carlitosjft

    Joined:
    Mar 18, 2016
    Posts:
    7
    So I am new, I am trying to follow this tuto;


    The problem is that when I import the code to the character it does not add the Player Input automatically and also it doesnt move at all when I press neither the controller nor the keyboard, Ive checked the code so many times but ill paste it here

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    using UnityEngine.InputSystem;

    [RequireComponent(typeof(CharacterController))]
    [RequireComponent(typeof(PlayerInput))]

    public class TwinStickMovement : MonoBehaviour
    {
    [SerializeField] private float playerSpeed = 5f;
    [SerializeField] private float gravityValue = -9.81f;
    [SerializeField] private float controllerDeadzone = 0.1f;
    [SerializeField] private float gamepadRotateSmoothing = 1000f;

    [SerializeField] private bool isGamepad;

    private CharacterController controller;

    private Vector2 movement;
    private Vector2 aim;

    private Vector3 playerVelocity;

    private PlayerControls playerControls;
    private PlayerInput playerInput;

    private void Awake()

    {
    controller = GetComponent<CharacterController>();
    playerControls = new PlayerControls();
    playerInput = GetComponent<PlayerInput>();
    }

    private void OnEnable()
    {
    playerControls.Enable();
    }

    private void OnDisable()
    {
    playerControls.Disable();
    }

    void Update()
    {
    HandleInput();
    HandleMovement();
    HandleRotation();
    }

    void HandleInput()
    {
    movement = playerControls.Controls.Movement.ReadValue<Vector2>();
    aim = playerControls.Controls.Aim.ReadValue<Vector2>();
    }

    void HandleMovement()
    {
    Vector3 move = new Vector3(movement.x, 0, movement.y);
    controller.Move(move * Time.deltaTime * playerSpeed);

    playerVelocity.y += gravityValue * Time.deltaTime;
    controller.Move(playerVelocity * Time.deltaTime);
    }

    void HandleRotation()
    {

    }


    }
     
  2. Carlitosjft

    Carlitosjft

    Joined:
    Mar 18, 2016
    Posts:
    7
    Btw I tried adding the Player Input manually but still wont work :(