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 protonhero · Jan 23, 2020 at 01:08 PM · physics3dplayer movementfirst-person-controllerfirst-person

Physics based player controller problems

My physics based player controller is slippery awkward and the player cant go faster than its max speed when affected by outside forces like and explosion is there any way to fix these problems?


Code:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class PlayerController : MonoBehaviour
 {
     // Movement Variables
     private Vector3 movement;
     public float speed;
     public float groundSpeed = 50f;
     public float airSpeed = 20f;
     public float maxSpeed = 10f;
     public float jumpForce = 5f;
     public float groundDistance = 1.1f;
     public Rigidbody rb;
 
     // Camera Variables
     public float sensitivity = 200;
     public Transform player;
     public Transform cam;
     private float xRotation = 0f;
 
     // Runs when the game starts
     private void Start()
     {
         rb = GetComponent<Rigidbody>();
         Cursor.lockState = CursorLockMode.Locked;
     }
 
     private void Update()
     {
         // Runs Camera Function
         Look();
     }
 
     // Runs every physics update
     private void FixedUpdate()
     {
         // Runs Movement Function
         Movement();
     }
 
     // Does Movment Stuff
     private void Movement()
     {
         // AddsForce to move the player using wasd or arrow keys
         float x = Input.GetAxisRaw("Horizontal");
         float z = Input.GetAxisRaw("Vertical");
 
         rb.AddForce(transform.forward * z * speed);
         rb.AddForce(transform.right * x * speed);
 
         // Changes player's acceleration in the air
         if (IsGrounded() == false)
         {
             speed = airSpeed;
         }
         if (IsGrounded() == true)
         {
             speed = groundSpeed;
         }
 
         // Uses rb.velocity to jump instead of addforce
         if (Input.GetButton("Jump") && IsGrounded() == true)
         {
             rb.velocity = new Vector3(rb.velocity.x, jumpForce, rb.velocity.z);
         }
 
         // Limits Speed
         if (rb.velocity.magnitude > maxSpeed)
         {
             rb.velocity = rb.velocity.normalized * maxSpeed;
         }
     }
 
     // Checks If Player Is Grounded
     private bool IsGrounded()
     {
         return Physics.Raycast(transform.position, Vector3.down, groundDistance);
     }
 
     // Does First Person Camera Stuff
     private void Look()
     {
         float mouseX = Input.GetAxisRaw("Mouse X") * sensitivity * Time.deltaTime;
         float mouseY = Input.GetAxisRaw("Mouse Y") * sensitivity * Time.deltaTime;
 
         xRotation -= mouseY;
         xRotation = Mathf.Clamp(xRotation, -90f, 90f);
 
         cam.localRotation = Quaternion.Euler(xRotation, 0f, 0f);
         player.Rotate(Vector3.up * mouseX);
     }
 }


Edit: I'm also looking for an effective coded gravity solution that works much like the default unity gravity

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

Answer by JasonBennett · Jan 23, 2020 at 03:51 PM

Hello @protonhero,

First off, kudos to you for commenting your code! That's a good practice. Secondly, this bit of code is your problem regarding the explosions:

 // Limits Speed
          if (rb.velocity.magnitude > maxSpeed)
          {
              rb.velocity = rb.velocity.normalized * maxSpeed;
          }



Any time you adjust the velocity on a Rigidbody directly it is going to affect the Rigidbody ANY time it has a velocity. The forces on it become superfluous since you are directly controlling the velocity.

A better way to achieve what you want is to limit the movement force when the player is above a certain velocity.

 // AddsForce to move the player using wasd or arrow keys
          float x = Input.GetAxisRaw("Horizontal");
          float z = Input.GetAxisRaw("Vertical");
  
 //MOVED FROM BELOW
 if (rb.velocity.magnitude < maxSpeed)
 {
          rb.AddForce(transform.forward * z * speed);
          rb.AddForce(transform.right * x * speed);
 }



Notice that I switched the value comparison so that you are checking if you are below maxSpeed before adding the force.

As for the "slippery" movement, I would play around with the different ForceModes. The AddForce method will have some overloads that allow you to specify the ForceMode. Impulse or Acceleration might get you the effect you desire.

Side note: I'm not entirely sure what the poster above me is suggesting, but my cursory reading of their comment raises suspicion. Generally you do NOT want to change the mass of a Rigidbody during runtime unless your game actually calls for a change in mass (i.e. something shrinks). Otherwise the physics can get nutty. I haven't tested their solution, though, so I am not 100% recommending against it. I am just suspicious of their method. (Sometimes you have to "hack" things to get them to work, but I don't think you need to in this case.)

Let me know if you have any additional questions or need something explained.

-Jason

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 lgarczyn · Jan 24, 2020 at 03:52 AM 0
Share

There is an issue with the updated code, you will not be able to move in any direction, including to slow down, if you ever reach maximum speed. A better way to do it would be:

 Vector3 forces = new Vector3(x, 0, z) * Time.fixedDeltaTime;
 Vector3 targetVel = rb.velocity + forces;
 float maxSpeed = $$anonymous$$athf.$$anonymous$$ax(speed, rb.velocity.magnitude);
 rb.velocity = Vector3.Clamp$$anonymous$$agnitude(targetVel, maxVel);
 

To add gravity, change the force line to this:

 Vector3 forces = new Vector3(x, customGravity, z) * Time.fixedDeltaTime;
avatar image junctionboss · Jan 28, 2020 at 07:17 PM 0
Share

I got an email that this post should , is, here:

"I mean... this is Unity Answers. Everybody is reasoning from the point of view of insufficient experience and trying to learn more. It is fully within bounds to ask for an explanation of an answer's logic. If you aren't willing to explain your logic to somebody you deem as having "insufficient experience", then you either don't understand the logic, or are posting in the wrong forum. "

Was it removed for some reason ? Be nice to know Im not getting out of date messages, for nothing in my email folder ;

Thx

avatar image lgarczyn junctionboss · Jan 30, 2020 at 04:00 AM 0
Share

The post is not removed, but maybe an old answer was?

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

232 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 avatar image avatar image avatar image avatar image avatar image 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

Player Movement in unity3d 1 Answer

How to move the rotation of the X and Z axis of a 3D model's head with Mouse Input? 0 Answers

How to shake/wobble camera on movement?(3D First Person),How to add a small wobble or shake to my camera when walking(3D First Person) 0 Answers

How to trigger scripts with line sight. (3D & First-Person) 1 Answer

How to walk around a 3D sphere? 0 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