Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 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
2
Question by MMKnight · Jul 17, 2021 at 02:04 PM · rigidbodycharacter movementfps controller

My Rigidbody FPS controller is falling very slow, please help!

My movement script:

How it looks for me: [Youtube][1] BTW, my gravity is set to -19,62.

using System.Collections; using System.Collections.Generic; using UnityEngine;

public class PlayerMovement : MonoBehaviour { float playerHeight = 2f;

 [SerializeField] Transform orientation;

 [Header("Movement")]
 [SerializeField] float moveSpeed = 6f;
 [SerializeField] float airMultiplier = 0.4f;
 float movementMultiplier = 10f;

 [Header("Sprinting")]
 [SerializeField] float walkSpeed = 8f;
 [SerializeField] float sprintSpeed = 10f;
 [SerializeField] float acceleration = 10f;

 [Header("Jumping")]
 public float jumpForce = 10f;

 [Header("Keybinds")]
 [SerializeField] KeyCode jumpKey = KeyCode.Space;
 [SerializeField] KeyCode sprintKey = KeyCode.LeftShift;

 [Header("Drag")]
 [SerializeField] float groundDrag = 6f;
 [SerializeField] float airDrag = 0f;

 float horizontalMovement;
 float verticalMovement;

 [Header("Ground Detection")]
 [SerializeField] Transform groundCheck;
 [SerializeField] LayerMask groundMask;
 [SerializeField] float groundDistance = 0.2f;
 public bool isGrounded { get; private set; }

 Vector3 moveDirection;
 Vector3 slopeMoveDirection;

 Rigidbody rb;

 RaycastHit slopeHit;

 private bool OnSlope()
 {
     if (Physics.Raycast(transform.position, Vector3.down, out slopeHit, playerHeight / 2 + 0.5f))
     {
         if (slopeHit.normal != Vector3.up)
         {
             return true;
         }
         else
         {
             return false;
         }
     }
     return false;
 }

 private void Start()
 {
     rb = GetComponent<Rigidbody>();
     rb.freezeRotation = true;
 }

 private void Update()
 {
     isGrounded = Physics.CheckSphere(groundCheck.position, groundDistance, groundMask);

     MyInput();
     ControlDrag();
     ControlSpeed();

     if (Input.GetKeyDown(jumpKey) && isGrounded)
     {
         Jump();
     }

     slopeMoveDirection = Vector3.ProjectOnPlane(moveDirection, slopeHit.normal);
 }

 void MyInput()
 {
     horizontalMovement = Input.GetAxisRaw("Horizontal");
     verticalMovement = Input.GetAxisRaw("Vertical");

     moveDirection = orientation.forward * verticalMovement + orientation.right * horizontalMovement;
 }

 void Jump()
 {
     if (isGrounded)
     {
         rb.velocity = new Vector3(rb.velocity.x, 0, rb.velocity.z);
         rb.AddForce(transform.up * jumpForce, ForceMode.Impulse);
     }
 }

 void ControlSpeed()
 {
     if (Input.GetKey(sprintKey) && isGrounded)
     {
         moveSpeed = Mathf.Lerp(moveSpeed, sprintSpeed, acceleration * Time.deltaTime);
     }
     else
     {
         moveSpeed = Mathf.Lerp(moveSpeed, walkSpeed, acceleration * Time.deltaTime);
     }
 }

 void ControlDrag()
 {
     if (isGrounded)
     {
         rb.drag = groundDrag;
     }
     else
     {
         rb.drag = airDrag;
     }
 }

 private void FixedUpdate()
 {
     MovePlayer();
 }

 void MovePlayer()
 {
     if (isGrounded && !OnSlope())
     {
         rb.AddForce(moveDirection.normalized * moveSpeed * movementMultiplier, ForceMode.Acceleration);
     }
     else if (isGrounded && OnSlope())
     {
         rb.AddForce(slopeMoveDirection.normalized * moveSpeed * movementMultiplier, ForceMode.Acceleration);
     }
     else if (!isGrounded)
     {
         rb.AddForce(moveDirection.normalized * moveSpeed * movementMultiplier * airMultiplier, ForceMode.Acceleration);
     }
 }

}

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

2 Replies

· Add your reply
  • Sort: 
avatar image
2
Best Answer

Answer by DenisIsDenis · Jul 17, 2021 at 02:10 PM

In the settings of your Rigidbody, you set the Drag equal to 2. In fact, this slows down the fall. Set it to zero and the drop will be faster.

Comment
Add comment · Show 2 · 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 DenisIsDenis · Jul 18, 2021 at 03:21 AM 0
Share

It did work but now if I jump and hold down "W" i fly forward.

@MMKnight, I tested your script and found out the reason. You limited the player's speed through drag, but when you made a drag equal to 0 in the air, nothing prevented the player's infinite acceleration, he could move at any speed.

I suggest limiting the player's movement speed manually, just change your FixedUpdate() to the following lines:

 private void FixedUpdate ()
 {
     MovePlayer ();

     float maxSpeed = 7;             // Maximum speed (better to make it global)
     Vector3 velocity = rb.velocity; // Create a velocity vector with which we will work
     velocity.y = 0;                // Zero the y-axis because we want to keep the falling speed.
     velocity = Vector3.ClampMagnitude(velocity, maxSpeed); // Limiting the speed of movement 
     velocity.y = rb.velocity.y;     // Returning the y-axis value
     rb.velocity = velocity;         // And change the velocity of Rigidbody
 }

(Note: if the player's movement occurs with a little twitching, then enable motion interpolation in the settings of your Rigidbody).

avatar image MMKnight · Jul 18, 2021 at 09:48 AM 0
Share

Thank you so much!

avatar image
2

Answer by b1gry4n · Jul 17, 2021 at 02:07 PM

you are setting your air drag to 2f, thats pretty high. try 0.01f and go up from there

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 MMKnight · Jul 17, 2021 at 06:43 PM 1
Share

It did work but now if I jump and hold down "W" i fly forward.

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

172 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

Related Questions

How can I add smooth movement to a RigidBody? 2 Answers

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

Having trouble turning a Transform movement into a Rigidbody force. Code included 1 Answer

How would I go about keeping velocity in the air with this but allowing it to be controlled slightly in air? 1 Answer

How can I get a responsive rigidbody FPS controller without acceleration that reacts to forces? 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