Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
4 captures
13 Jun 22 - 14 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 MegaBomb_Unity · Oct 09, 2021 at 02:14 AM · inputmovement scriptkeyboard inputcontext

Something weird with my input system

Ok. I followed this tutorial for 3d movement in unity. Here is the code for movement: using UnityEngine;

 [SerializeField] CharacterController controller;
 [SerializeField] float speed = 15f;
 Vector2 horizontalInput;

 [SerializeField] float jumpHeight = 3.5f;
 bool jump;

 [SerializeField] float gravity = -30f;
 Vector3 verticalVelocity = Vector3.zero;
 [SerializeField] LayerMask groundMask;
 bool isGrounded;

 private void Update()
 {
     isGrounded = Physics.CheckSphere(transform.position, 0.1f, groundMask);
     if (isGrounded)
     {
         verticalVelocity.y = 0;
     }

     Vector3 horizontalVelocity = (transform.right * horizontalInput.x + transform.forward * horizontalInput.y) * speed;
     controller.Move(horizontalVelocity * Time.deltaTime);

     // Jump: v = sqrt(-2 * jumpHeight * gravity)
     if (jump)
     {
         if(isGrounded)
         {
             verticalVelocity.y = Mathf.Sqrt(-2f * jumpHeight * gravity);
         }
         jump = false;
     }

     verticalVelocity.y += gravity * Time.deltaTime;
     controller.Move(verticalVelocity * Time.deltaTime);
 }

 public void RecieveInput (Vector2 _horizontalInput)
 {
     horizontalInput = _horizontalInput;
 }

 public void OnJumpPressed ()
 {
     jump = true;
 }

} and here is the code for the input manager:

 [SerializeField] Movement movement;
 [SerializeField] MouseLook mouseLook;
 [SerializeField] GunMechanics gun;

 PlayerControls controls;
 PlayerControls.GroundMovementActions groundMovement;

 Vector2 horizontalInput;
 Vector2 mouseInput;

 private void Awake()
 {
     controls = new PlayerControls();
     groundMovement = controls.GroundMovement;

     // groundMovement.[action].performed += context => do something
     groundMovement.HorizontalMovement.performed += ctx => horizontalInput = ctx.ReadValue<Vector2>();

     groundMovement.Jump.performed += _ => movement.OnJumpPressed();

     groundMovement.MouseX.performed += ctx => mouseInput.x = ctx.ReadValue<float>();
     groundMovement.MouseY.performed += ctx => mouseInput.y = ctx.ReadValue<float>();

     groundMovement.Shoot.performed += _ => gun.Shoot();
 }

 private void Update()
 {
     movement.RecieveInput(horizontalInput);
     mouseLook.RecieveInput(mouseInput);
 }

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

 private void OnDestroy()
 {
     controls.Disable();
 }

}

While this code works, I found a problem with it. When I spam the WASD a bunch of times, both mouseLook and movement get stuck for some reason. As in, I am stuck moving in one direction and can't rotate the camera until I input the same direction I am stuck in.

This is a huge problem when implementing this movement in a movement shooter. Is there any way to fix this?

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 MegaBomb_Unity · Oct 11, 2021 at 10:44 AM

Nevermind. Found the solution all by myself. Just turn the controller.move script into an if statement.

     verticalVelocity.y += gravity * Time.deltaTime;
     if (verticalVelocity != Vector3.zero) {
         controller.Move(verticalVelocity * Time.deltaTime);
     }
Comment
Add comment · Show 1 · 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
avatar image MegaBomb_Unity · Oct 11, 2021 at 10:44 AM 0
Share

At least, I think this solution works.

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

166 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

Related Questions

How do I have multiple controls for the same function? (Controller and Keyboard) 1 Answer

Unity input system: TapInteraction does not produce a value when using ReadValue() 0 Answers

WebGL - Module.doNotCaptureKeyboard: true or Module.keyboardListeningElement: ... NOT WORKING ANYMORE 0 Answers

How can I smooth out and unlock the movement with the input system? 0 Answers

Why does the keyboard control what button is selected? 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