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 /
  • Help Room /
avatar image
0
Question by CockCOckIsYes · Jul 09, 2021 at 07:36 PM · rigidbodycharactercontrollerrigidbody.addforcegeneral

same script, different results every test run

Hi, I was trying to create a character controller and I had a problem where when I try to walk it'd neither go sonic speed or just not move at all. Yesterday I couldn't even get it to move but this morning it moves way too fast. Here's the code (don't mind the jumping part it's bad and ill fix it later): using System.Collections; using System.Collections.Generic; using UnityEngine;

 public class Controller : MonoBehaviour
 {
     private Rigidbody rigidbody;
 
     public float walkSpeed;
     public float jumpPower;
 
     void Awake()
     {
         rigidbody = gameObject.GetComponent<Rigidbody>();
     }
 
     // Update is called once per frame
     void Update()
     {
 
         float walkMovement = 0;
         float strafeMovement = 0;
 
         if (Input.GetKey(KeyCode.W))
         {
             walkMovement = walkSpeed;
         }
         if (Input.GetKey(KeyCode.S))
         {
             walkMovement = -walkSpeed;
         }
         // --------------------------------------------------------------------
         if (Input.GetKey(KeyCode.A))
         {
             strafeMovement = walkSpeed;
         }
         if (Input.GetKey(KeyCode.D))
         {
             strafeMovement = -walkSpeed;
         }
         // --------------------------------------------------------------------
         if (Input.GetKeyDown(KeyCode.Space))
         {
             rigidbody.AddForce(0, jumpPower, 0);
         }
 
         // Move character
         rigidbody.AddRelativeForce(Vector3.forward * walkMovement);
         rigidbody.AddRelativeForce(Vector3.left * strafeMovement);
     }
 }
 
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 DenisIsDenis · Jul 10, 2021 at 02:43 AM

Replace Update() with FixedUpdate(). Your problem is related to the frames per second. But the more fps, the more often the force is applied and the higher the speed of the character. FixedUpdate() is called regardless of fps. Therefore, any device will always have the same speed.

Note: in your script, the player can speed up infinitely. To prevent this, enter the variable

 public float maxSpeed = 10;

and write the following code at the end of the FixedUpdate() function:

 var velocity = rigidbody.velocity;
 velocity.y = 0;
 velocity = Vector3.ClampMagnitude (velocity, maxSpeed);
 velocity.y = rigidbody.velocity.y;
 rigidbody.velocity = velocity;
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 CockCOckIsYes · Jul 11, 2021 at 12:34 AM 0
Share

Doesn't quite work, It seems to me that it's a problem related to materials, as even when I use FixedUpdate, the controller sometimes doesn't move, but when I move when jumping, it moves perfectly fine. Apologies for not clearing that part up

avatar image DenisIsDenis CockCOckIsYes · Jul 11, 2021 at 02:16 AM 0
Share

Yes, physical materials can affect movement on the ground. But in this case, it is advisable to use FixedUpdate(). Try to write ForceMode.VelocityChange as the second argument in the AddRelativeForce() functions. Just keep in $$anonymous$$d that the acceleration will increase greatly, so after that reduce walkMovement and strafeMovement.

avatar image CockCOckIsYes DenisIsDenis · Jul 12, 2021 at 01:34 AM 0
Share

Ah thank you, I'll try that out

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

223 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

Related Questions

Gravity is not working 0 Answers

revolving a rigidbody around another MOVING rigidbody 0 Answers

Accelerate Player with Rigidbody.AddForce 0 Answers

Player getting stuck on collider/walls 0 Answers

Character Controller vs Rigidbody 3rd Person Zelda-esque 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