Search Unity

Third Person Controller with New Input System??

Discussion in 'Scripting' started by djc51401, Jun 20, 2020.

  1. djc51401

    djc51401

    Joined:
    Apr 3, 2020
    Posts:
    12
    Hi, i was watching this great tutorial on how to make a 3rd person controller in unity, Here's the link, and I'm trying to implement it with the new input system, but its very glitchy.

    Code (CSharp):
    1.  
    2. using System.Collections;
    3.  
    4. using System.Collections.Generic;
    5.  
    6. using UnityEngine;
    7.  
    8. using UnityEngine.InputSystem;
    9.  
    10.  
    11.  
    12. public class PlayerController : MonoBehaviour
    13.  
    14. {
    15.  
    16. public float walkSpeed;
    17.  
    18. public float runSpeed;
    19.  
    20.  
    21.  
    22. Vector2 input;
    23.  
    24. bool running;
    25.  
    26.  
    27.  
    28. float turnSmoothTime = .2f;
    29.  
    30. float turnSmoothSpeed;
    31.  
    32.  
    33.  
    34. float speedSmoothTime = .1f;
    35.  
    36. float speedSmoothVelocity;
    37.  
    38. float currentSpeed;
    39.  
    40.  
    41.  
    42. Transform camt;
    43.  
    44.  
    45.  
    46. void Start()
    47.  
    48. {
    49.  
    50. camt = Camera.main.transform;
    51.  
    52. }
    53.  
    54.  
    55.  
    56. void Update()
    57.  
    58. {
    59.  
    60. Move();
    61.  
    62. }
    63.  
    64.  
    65.  
    66. void Move()
    67.  
    68. {
    69.  
    70. Vector2 inputDir = input.normalized;
    71.  
    72. if(inputDir != Vector2.zero)
    73.  
    74. {
    75.  
    76. float targetRotation = Mathf.Atan2(inputDir.x, inputDir.y) * Mathf.Rad2Deg * camt.eulerAngles.y;
    77.  
    78. transform.eulerAngles = Vector3.up * Mathf.SmoothDampAngle(transform.eulerAngles.y, targetRotation, ref turnSmoothSpeed, turnSmoothTime);
    79.  
    80. }
    81.  
    82. else
    83.  
    84. {
    85.  
    86. running = false;
    87.  
    88. }
    89.  
    90.  
    91.  
    92. float targetSpeed = (running ? runSpeed : walkSpeed) * inputDir.magnitude;
    93.  
    94. currentSpeed = Mathf.SmoothDamp(currentSpeed, targetSpeed, ref speedSmoothVelocity, speedSmoothTime);
    95.  
    96.  
    97.  
    98. transform.Translate(transform.forward * currentSpeed * Time.deltaTime, Space.World);
    99.  
    100. }
    101.  
    102.  
    103.  
    104. public void OnMove(InputAction.CallbackContext context)
    105.  
    106. {
    107.  
    108. input = context.ReadValue<Vector2>();
    109.  
    110. }
    111.  
    112.  
    113.  
    114. public void OnJump(InputAction.CallbackContext context)
    115.  
    116. {
    117.  
    118.  
    119.  
    120. }
    121.  
    122.  
    123.  
    124. public void OnRun(InputAction.CallbackContext context)
    125.  
    126. {
    127.  
    128. running = context.performed;
    129.  
    130. }
    131.  
    132. }


    This is my script after I finished, but with this script, the character doesn't quite move with the camera, it does sometimes but other times it'll just turn itself around and do weird things. I also got the camera script,


    Code (CSharp):
    1.  
    2. using System.Collections;
    3.  
    4. using System.Collections.Generic;
    5.  
    6. using UnityEngine;
    7.  
    8. using UnityEngine.InputSystem;
    9.  
    10.  
    11.  
    12. public class ThirdPersonCamera : MonoBehaviour
    13.  
    14. {
    15.  
    16. public Transform target;
    17.  
    18. public float distanceFromTarget;
    19.  
    20.  
    21.  
    22. public float xSensitivity;
    23.  
    24. public float ySensitivity;
    25.  
    26.  
    27.  
    28. public Vector2 pitchMinMax = new Vector2(-40, 85);
    29.  
    30.  
    31.  
    32. public float rotationSmoothTime = 1.2f;
    33.  
    34. Vector3 rotationSmoothSpeed;
    35.  
    36. Vector3 currentRotation;
    37.  
    38.  
    39.  
    40. float yaw;
    41.  
    42. float pitch;
    43.  
    44.  
    45.  
    46. // Start is called before the first frame update
    47.  
    48. void Start()
    49.  
    50. {
    51.  
    52. }
    53.  
    54.  
    55.  
    56. // Update is called once per frame
    57.  
    58. void LateUpdate()
    59.  
    60. {
    61.  
    62. Vector3 targetRotation = new Vector3(pitch, yaw);
    63.  
    64. transform.eulerAngles = targetRotation;
    65.  
    66. pitch = Mathf.Clamp(pitch, pitchMinMax.x, pitchMinMax.y);
    67.  
    68.  
    69.  
    70. currentRotation = Vector3.SmoothDamp(currentRotation, new Vector3(pitch, yaw), ref rotationSmoothSpeed, rotationSmoothTime);
    71.  
    72.  
    73.  
    74. transform.position = target.position - transform.forward * distanceFromTarget;
    75.  
    76. }
    77.  
    78.  
    79.  
    80. public void OnLook(InputAction.CallbackContext context)
    81.  
    82. {
    83.  
    84. yaw += context.ReadValue<Vector2>().x * xSensitivity * Time.deltaTime;
    85.  
    86. pitch -= context.ReadValue<Vector2>().y * ySensitivity * Time.deltaTime;
    87.  
    88. }
    89.  
    90. }
    91.  
    and again a similar issue, turning the camera, even with the damping, is still choppy and it doesn't work the greatest. does anyone know how i can stop this and fix it? or is it more of a problem with the input system itself as its not finished yet?

    Any help is appreciated!