- Home /
New input system & character controller
Hi, i'm having problems with new input system and characterController. The problem here is that my player just floats to a considerable height when i start the game, and i can't manage to make the input system and character controller to work together, but when i use rigidbodies it magically starts to work. Here's my code
using UnityEngine; #if ENABLE_INPUT_SYSTEM using UnityEngine.InputSystem; #endif
public class Player : MonoBehaviour { #region Variables
//? Serialized variables
[Header("Player")]
[Tooltip("The sprint speed of the player")]
public float sprintSpeed;
[Tooltip("The standard moving speed of the player")]
public float moveSpeed;
public float speedOffset = 0.1f;
[Tooltip("Acceleration and deceleration")]
private float speedChangeRate = 10.0f;
[Space(10)]
[Tooltip("The height to jump")]
public float jumpHeight = 1.2f;
//? Unserialized variables
private const float g = -9.81f;
private const float _terminalVelocity = 102f; //Calculated for 90kg of mass, humanoid shape
private CharacterController _playerController;
private InputSheet _playerInput;
private float _verticalVelocity;
private float _jumpCooldownDelta;
private bool _grounded;
private float _actualSpeed;
private float _jumpCooldown = 1f;
#endregion
private void Awake()
{
_playerController = GetComponent<CharacterController>();
_playerInput = new InputSheet();
_playerInput.Player.Enable();
_playerInput.Player.Jump.performed += Jump;
_playerInput.Player.Move.performed += Move;
}
private void Start()
{
//Reset cooldown
_jumpCooldownDelta = _jumpCooldown;
}
private void Update()
{
_grounded = transform.Find("GroundCheck").GetComponent<GroundCheck>().isGrounded;
}
private void Move(InputAction.CallbackContext context)
{
//Get movement vector
Vector2 inputVector = context.ReadValue<Vector2>();
//Choose speed based on if sprint input is pressed
float _targetSpeed = _playerInput.Player.Sprint.triggered ? sprintSpeed : moveSpeed;
float horizontalSpeed = new Vector3(_playerController.velocity.x, 0.0f, _playerController.velocity.z).magnitude;
if(horizontalSpeed < _targetSpeed - speedOffset || horizontalSpeed > _targetSpeed + speedOffset)
{
Debug.Log("Target speed:" + _targetSpeed);
//Creates curved result for more organical shape, and rounds to 3 decimal places
_actualSpeed = Mathf.Round(Mathf.Lerp(horizontalSpeed, _targetSpeed, speedChangeRate * Time.deltaTime) * 1000f) / 1000f;
Debug.Log("speed:" + _actualSpeed);
}
_playerController.Move(new Vector3(inputVector.x, _playerController.velocity.y, inputVector .y) * _actualSpeed);
}
private void Jump(InputAction.CallbackContext context)
{
//If <jump button> is pressed and cooldown has passed and is grounded, Jump
if(context.performed && _grounded && _jumpCooldownDelta <= 0.0f)
{
//Formula to calculate the speed needed to reach requested point
_verticalVelocity = Mathf.Sqrt(jumpHeight * -2f * g);
//Add the force and reset cooldown
_playerController.Move(new Vector3(0.0f, _verticalVelocity, 0.0f));
}
}
private void ApplyGravity()
{
if(_grounded)
{
//Stop from falling indefinitely
if(_verticalVelocity < 0.0f)
{
_verticalVelocity = -2f;
}
//Process jump cooldown
if(_jumpCooldownDelta > 0.0f)
_jumpCooldownDelta -= Time.deltaTime;
}
else
{
if(_verticalVelocity < _terminalVelocity)
_verticalVelocity += g * Time.deltaTime;
//Reset cooldown
_jumpCooldownDelta = _jumpCooldown;
}
}
}
P.S: _grounded is not part of CharacterController, but it isn't the issue since it works with rigidbody.
Thanks in advance for all the answers.
Answer by krengdroid · Mar 25 at 07:47 AM
so you have that applygravity() method, but i cant find where you call it. pretty sure thats the issue
(also, _verticalVelocity in the applygravity() will be negative if going down, but _terminalVelocity is positive, so _verticalVeclotity is always less than _terminalVelocity)
Your answer
Follow this Question
Related Questions
New unity input system problems 0 Answers
When using input, array.Add replaces the last member 1 Answer
Help In Making a SphereCast for 3D Tire! Working RayCast Script included! 0 Answers
Problem with input 1 Answer
Why moveDirection.z doesn't work? 2 Answers