Search Unity

Help with new input system... please

Discussion in 'Getting Started' started by cvusmo, Feb 1, 2020.

  1. cvusmo

    cvusmo

    Joined:
    Jun 11, 2019
    Posts:
    5
    02/02/2020 I've worked out the code a bit. I'm able to move the character around the map. I'm not able to have the camera follow the character, or have the character go into an idle state. I'm working on figuring out how to make an idle state as well as getting the camera to follow the character. This has taken me two days to figure out. ANY help from you guys would be greatly appreciated. This project is going to be my first game. UNLIKE what everyone has said, "Dont do an RPG for your first game," I'm going to go big. I'm making a simple top down 2D RPG where it only focuses on a menu, three quests, a few NPCs, and one map. Once I have that, I hope to have a better understanding of how this works. Any tips on shortening code or suggestions would be appreciated.




    EDIT: 02/01/2020 is below
    Screenshot (9).png This is how I've set everything up in unity. You can see what I've attached to "Player". Any changes I need to make? I stumbled upon making animations work.



    I'm wanting to learn how to use this new input system. I've looked at multiple YouTubers for various code on using the new input system. I've read about V 1.0.0 4 on Unity. I've figured out how to somewhat move my character. I'm wanting to use the Rigidbody2D physics systems for movement as eventually I want the characters "weight" (mass) to slow him down. I can't figure out why he will move but return to his origin.

    I named my classes a bit differently but here's the code I've pieced together after watching a lot of tutorials:
    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.InputSystem;
    3.  
    4. public class PlayerMove : MonoBehaviour
    5. {
    6. PlayerControls controls;
    7. public Camera mainCamera;
    8. InputActionAsset inputAction;
    9. Vector2 movementInput;
    10. Vector2 velocity;
    11.  
    12.  
    13. public float speed = 4.5f;
    14. public Rigidbody2D playerRigidbody2D;
    15. private Vector2 inputDirection;
    16. private Vector2 movement;
    17.  
    18. private Plane playerMovementPlane;
    19. void Awake()
    20. {
    21. controls = new PlayerControls();
    22. controls.Player.Movement.performed += ctx => movementInput = ctx.ReadValue<Vector2>();
    23. }
    24.  
    25. void FixedUpdate()
    26. {
    27. float h = movementInput.x;
    28. float v = movementInput.y;
    29. Debug.Log(h);
    30. Debug.Log(v);
    31.  
    32.  
    33. var targetInput = new Vector2(h, v);
    34. inputDirection = Vector2.Lerp(inputDirection, targetInput, Time.deltaTime * 10f);
    35.  
    36. MoveThePlayer(inputDirection);
    37. var cameraForward = mainCamera.transform.forward;
    38. var cameraRight = mainCamera.transform.right;
    39.  
    40. cameraForward.y = 0f;
    41. cameraRight.y = 0f;
    42.  
    43. }
    44.  
    45. void MoveThePlayer(Vector2 desiredDirection)
    46. {
    47. movement.Set(desiredDirection.x, desiredDirection.y);
    48.  
    49. movement = movement * speed * Time.deltaTime * 10f;
    50. playerRigidbody2D.MovePosition(desiredDirection);
    51.  
    52.  
    53. }
    54.  
    55.  
    56. void OnEnable()
    57. {
    58. controls.Enable();
    59. }
    60.  
    61. void OnDisable()
    62. {
    63. controls.Disable();
    64. }
    65. }
    66.  
     
    Last edited: Feb 3, 2020