Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by marcibel07 · Mar 23 at 11:43 PM · errorinputcharactercontroller

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.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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)

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

215 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges