Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 unity_Vpi53N_fb99F6g · Feb 22, 2020 at 04:05 PM · rigidbodynoobfps controller

Rigidbody controller counter movement

Basically I'm doing a FPS controller with a rigidbody, since I want the movement to have physics involved. But I'm kind of a noob and I am not able to add some counter movement so when the player is on the ground, they can controll themselves better and not slide around everywhere. This is what I currently have: #region Variables //Camera public CameraMovement camera;

     //Movement
     public int speed = 20;
     public int maxSpeed = 20;
     public int slowDownFactor = 10;
     private float hMovement;
     private float vMovement;
     private Vector2 movement;
     private Vector2 prevMovement;
     private bool jumping;
 
     //Jumping
     public int jumpForce = 10;
     public int extraGravity = 10;
 
     //Player
     private Rigidbody player;
     private CapsuleCollider collider;
 
     #endregion
 
     #region Unity Callbacks
     // Start is called before the first frame update
     void Start()
     {
         player = GetComponent<Rigidbody>();
         collider = GetComponent<CapsuleCollider>();
     }
 
     // Update is called once per frame
     void FixedUpdate()
     {
         Movement();
         //CounterMovement();
         Rotate();
 
         if (Input.GetKeyDown(KeyCode.Q)) Debug.Log(Grounded());
     }
     #endregion
 
     #region My methods
 
     //The player movement
     private void Movement()
     {
         //Gets the input
         hMovement = Input.GetAxisRaw("Horizontal");
         vMovement = Input.GetAxisRaw("Vertical");
 
         movement = new Vector2(hMovement, vMovement);
 
         //Jumping
         jumping = Input.GetButton("Jump");
 
         //If the movement is more than the maximum, cancel out the input(x)
         if (player.velocity.x > maxSpeed && hMovement > 0) hMovement = 0;
         if (player.velocity.x < -maxSpeed && hMovement < 0) hMovement = 0;
 
 
         //If the movement is more than the maximum, cancel out the input(z)
         if (player.velocity.z > maxSpeed && vMovement > 0) vMovement = 0;
         if (player.velocity.z < -maxSpeed && vMovement < 0) vMovement = 0;
 
         //Jumping
         if (jumping && Grounded())
         {
             player.AddForce(new Vector3(0, jumpForce, 0), ForceMode.Impulse);
         }
 
         //Adds the force to the rigidbody
         player.AddForce(hMovement * transform.right * Time.deltaTime * speed, ForceMode.Impulse);
         player.AddForce(vMovement * transform.forward * Time.deltaTime * speed, ForceMode.Impulse);
 
         //Extra gravity
         player.AddForce(new Vector3(0, -extraGravity, 0));
     }
 
     //The counter movement, to avoid sloppy movement
     private void CounterMovement()
     {
         Vector3 counterMovement = new Vector3(-player.velocity.x, 0, -player.velocity.z);
 
         if ((prevMovement.x != movement.x || prevMovement.y != movement.y) && Grounded())
         {
             
             //Add some counter movement if the player has stopped pressing a movement key, or pressed a new one
             player.AddForce(counterMovement * 40);
             prevMovement = movement;
         }
 
     }
 
 
     //Rotates the player on the y with the mouse
     private void Rotate()
     {
         transform.rotation = camera.playerRotation;
     }
 
     //Determins if the player is grounded
     private bool Grounded()
     {
         return Physics.Raycast(transform.position, Vector3.down, collider.bounds.extents.y + .1f);
     }
 
     #endregion

I've been stuck on this for a long time, so I finally decided to ask for help. Thanks in advance.

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
Best Answer

Answer by alwayscodeangry · Feb 22, 2020 at 05:49 PM

Have you tried adding a PhysicsMaterial to your ground surface collider? That should allow you to increase the surface friction values which would hopefully achieve the effect you're describing. It would also play nice with the physics engine which is always a nice bonus :)
 

Edit: My other suggestion would be to set the rigidbody's linear drag to a suitably high value when you want to slow it down rapidly:

 if ((prevMovement.x != movement.x || prevMovement.y != movement.y) && Grounded())
 {
     player.drag = COUNTER_DRAG;
 }
 else
 {
     player.drag = DEFAULT_DRAG; // probably 0.0f, but it's always good to avoid those hard coded constants...
 }
Comment
Add comment · Show 3 · 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 unity_Vpi53N_fb99F6g · Feb 22, 2020 at 07:56 PM 0
Share

That's what I initially thought to do but the thing is, it slows down the overall movement, so I have it on very low friction. What I want is the player to be able to change directions and come to a stop rapidly, because right now the movement is too heavy and uncontrollable.

avatar image alwayscodeangry unity_Vpi53N_fb99F6g · Feb 23, 2020 at 01:05 AM 0
Share

I've added a second option, hopefully that works for you :)

avatar image unity_Vpi53N_fb99F6g alwayscodeangry · Apr 28, 2020 at 06:44 PM 0
Share

Works like a charm. Thanks.

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

168 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

Related Questions

Rigidbody FPS controller? 0 Answers

Laggy FPS Camera (attached to rigid body player), I have tried everything. Unity wizards need to be summoned for this one. 0 Answers

How do I convert my FPS controller movement script to a rigidbody to prevent clipping. 0 Answers

How can I get a responsive rigidbody FPS controller without acceleration that reacts to forces? 2 Answers

My Rigidbody FPS controller is falling very slow, please help! 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