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 Hez · Jan 05, 2017 at 05:00 AM · c#rigidbodycharactercontroller

Speed up with Rigidbody as frame rate increases c#

I am using a Rigidbody controller and it has a habit of speeding up when the framerates increase. I attempted to fix this by using * time.deltatime, and that did make the increase in speed not as extreme but still there.

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.UI;
 
 [RequireComponent(typeof(Rigidbody))]
 
 public class Movment : MonoBehaviour
 {
     //.ToString("F1")
     [Header("Speed Setting")]
     public float speed = 10.0f;
     public float gravity = 10.0f;
     public float maxVelocityChange = 10.0f;
     public float Torquespeed = 0f;
     public float maxTorqueChange = 0f;
     public bool canJump = true;
     public float jumpHeight = 2.0f;
 
     [Header("Information Output")]
     public Vector3 velocity;
     public Vector3 torque;
     public Text Torque;
     public Text Speed;
     public Text Location;
 
     private bool grounded = false;
     Rigidbody rigidbody;   
     Vector3 targetVelocity = new Vector3();
     Vector3 targetTorque = new Vector3();
     void Awake()
     {
         rigidbody = this.GetComponent<Rigidbody>();
         rigidbody.freezeRotation = false;
         rigidbody.useGravity = false;
     }
 
     void FixedUpdate()
     {
         velocity = rigidbody.velocity;
         // Calculate how fast we should be moving
 
         //Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical")
         if (Input.GetAxis("Horizontal") > 0 && velocity.x < maxVelocityChange)
         { targetVelocity.x += Input.GetAxis("Horizontal");
             if (velocity.x  > maxVelocityChange * Time.deltaTime) { targetVelocity.x = maxVelocityChange * Time.deltaTime; }
         }
         if (Input.GetAxis("Horizontal") < 0 && velocity.x > -maxVelocityChange)
         { targetVelocity.x += Input.GetAxis("Horizontal");
             if (velocity.x  < -maxVelocityChange * Time.deltaTime) { targetVelocity.x = -maxVelocityChange * Time.deltaTime; }
         }
 
         if (Input.GetAxis("Vertical") > 0 && velocity.z < maxVelocityChange)
         { targetVelocity.z += Input.GetAxis("Vertical");
             if (velocity.z  > maxVelocityChange * Time.deltaTime) { targetVelocity.z = maxVelocityChange * Time.deltaTime; }
         }
         if (Input.GetAxis("Vertical") < 0 && velocity.z > -maxVelocityChange)
         { targetVelocity.z += Input.GetAxis("Vertical");
             if (velocity.z  < -maxVelocityChange * Time.deltaTime) { targetVelocity.z = -maxVelocityChange * Time.deltaTime; }
         }
         targetVelocity.y = 0;
         //targetTorque
 
           if (Input.GetAxis("Rotate Vertical") > 0 && Input.GetAxis("Rotate Vertical") > targetTorque.x)
         { targetTorque.x = Input.GetAxis("Rotate Vertical"); }
         if (Input.GetAxis("Rotate Vertical") < 0 && Input.GetAxis("Rotate Vertical") < targetTorque.x)
         { targetTorque.x = Input.GetAxis("Rotate Vertical"); }
 
         if (Input.GetAxis("Rotate Horizontal") > 0 && Input.GetAxis("Rotate Horizontal")  > targetTorque.y)
         { targetTorque.y = Input.GetAxis("Rotate Horizontal"); }
         if (Input.GetAxis("Rotate Horizontal") < 0 && Input.GetAxis("Rotate Horizontal")  < targetTorque.y)
         { targetTorque.y = Input.GetAxis("Rotate Horizontal"); }
         //Stop1
         if (Input.GetAxis("Jump") >0.5) {
             if (targetVelocity.x > 0) { targetVelocity.x -= speed; if (targetVelocity.x < 0) targetVelocity.x = 0; }
             if (targetVelocity.x < 0) { targetVelocity.x += speed; if (targetVelocity.x > 0) targetVelocity.x = 0; }
 
             if (targetVelocity.z > 0) { targetVelocity.z -= speed; if (targetVelocity.z < 0) targetVelocity.z = 0; }
             if (targetVelocity.z < 0) { targetVelocity.z += speed; if (targetVelocity.z > 0) targetVelocity.z = 0; }
             targetVelocity.y = 0;
 
             if (targetTorque.x > 0) { targetTorque.x -= Torquespeed; if (targetTorque.x < 0) targetTorque.x = 0; }
             if (targetTorque.x < 0) { targetTorque.x += Torquespeed; if (targetTorque.x > 0) targetTorque.x = 0; }
 
             if (targetTorque.y > 0) { targetTorque.y -= Torquespeed; if (targetTorque.y < 0) targetTorque.y = 0; }
             if (targetTorque.y < 0) { targetTorque.y += Torquespeed; if (targetTorque.y > 0) targetTorque.y = 0; }
             targetTorque.z = 0;
         }
         if (Input.GetAxis("Stop1") > 0.5)
         {        
             if (targetTorque.x > 0) { targetTorque.x -= Torquespeed; if (targetTorque.x < 0) targetTorque.x = 0; }
             if (targetTorque.x < 0) { targetTorque.x += Torquespeed; if (targetTorque.x > 0) targetTorque.x = 0; }
 
             if (targetTorque.y > 0) { targetTorque.y -= Torquespeed; if (targetTorque.y < 0) targetTorque.y = 0; }
             if (targetTorque.y < 0) { targetTorque.y += Torquespeed; if (targetTorque.y > 0) targetTorque.y = 0; }
             targetTorque.z = 0;
         }
         if (Input.GetAxis("Stop2") > 0.5)
         {
             if (targetVelocity.x > 0) { targetVelocity.x -= speed; if (targetVelocity.x < 0) targetVelocity.x = 0; }
             if (targetVelocity.x < 0) { targetVelocity.x += speed; if (targetVelocity.x > 0) targetVelocity.x = 0; }
 
             if (targetVelocity.z > 0) { targetVelocity.z -= speed; if (targetVelocity.z < 0) targetVelocity.z = 0; }
             if (targetVelocity.z < 0) { targetVelocity.z += speed; if (targetVelocity.z > 0) targetVelocity.z = 0; }
             targetVelocity.y = 0;
         }
 
         // Apply a force that attempts to reach our target velocity
         Vector3 ProsTargetVelocity = transform.TransformDirection(targetVelocity);
         ProsTargetVelocity *= speed;
         
         Vector3 velocityChange = (ProsTargetVelocity - velocity);
         velocityChange.x = Mathf.Clamp(velocityChange.x, -maxVelocityChange, maxVelocityChange);
         velocityChange.z = Mathf.Clamp(velocityChange.z, -maxVelocityChange, maxVelocityChange);
         velocityChange.y = 0;
         velocityChange *= Time.deltaTime;
 
         torque = rigidbody.angularVelocity;
         Vector3 ProsTargetTorque = transform.TransformDirection(targetTorque);
         ProsTargetTorque *= Torquespeed;        
         Vector3 TorqueChange = (ProsTargetTorque - torque);
         TorqueChange.x = Mathf.Clamp(TorqueChange.x, -maxTorqueChange, maxTorqueChange);
         TorqueChange.y = Mathf.Clamp(TorqueChange.y, -maxTorqueChange, maxTorqueChange);
         TorqueChange.z = 0;
         TorqueChange *= Time.deltaTime;
 
         Vector3 speedTrack = transform.InverseTransformDirection(velocity);
         rigidbody.AddForce(velocityChange, ForceMode.VelocityChange);
         rigidbody.AddTorque(TorqueChange, ForceMode.VelocityChange);
 
         Speed.text = "Speed: " + speedTrack.x.ToString("F1") + "," + speedTrack.y.ToString("F1") + "," + speedTrack.z.ToString("F1");
         Location.text = "Location: " + transform.position.x.ToString("F1") + "," + transform.position.y.ToString("F1") + "," + transform.position.z.ToString("F1");
         //Torque.text = "Torque: " + TorqueChange.x.ToString("F1") + "," + TorqueChange.y.ToString("F1") + "," + TorqueChange.z.ToString("F1");
 
         // We apply gravity manually for more tuning control
         //rigidbody.AddForce(new Vector3(0, -gravity * rigidbody.mass, 0));
 
         grounded = false;
     }
 
     void OnCollisionStay()
     {
         grounded = true;
     }
 
 
 }

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 Hez · Jan 08, 2017 at 03:56 AM

I had to completely rewrite the script. I am still not exactly sure what it was that was wrong, but I got it to work without speeding up upon framerate change. I feel kind of nubeish about a movement script keeping me down for a few days.

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.UI;
 
 [RequireComponent(typeof(Rigidbody))]
 
 public class Movment : MonoBehaviour
 {
     //.ToString("F1")
     [Header("Speed Setting")]
     public float speed = 10.0f;
     public float gravity = 10.0f;
     public float maxVelocityChange = 10.0f;
     public float Torquespeed = 0f;
     public float maxTorqueChange = 0f;
     public bool canJump = true;
     public float jumpHeight = 2.0f;
 
     [Header("Information Output")]
     public Vector3 velocity;
     public Vector3 torque;
     public Text Torque;
     public Text Speed;
     public Text Location;
 
     private bool grounded = false;
     Rigidbody rigidbody;   
     Vector3 targetVelocity = new Vector3();
     Vector3 targetTorque = new Vector3();
     void Awake()
     {
         rigidbody = this.GetComponent<Rigidbody>();
         rigidbody.freezeRotation = false;
         rigidbody.useGravity = false;
     }
 
     void FixedUpdate()
     {
         velocity = rigidbody.velocity;
         // Calculate how fast we should be moving
 
         //Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical")
         if (Input.GetAxis("Horizontal") != 0)
         { targetVelocity.x += Input.GetAxis("Horizontal");                       
         }    
         if (Input.GetAxis("Vertical") != 0)
         { targetVelocity.z += Input.GetAxis("Vertical");           
         }        
         targetVelocity.y = 0;
 
         //targetTorque
           if (Input.GetAxis("Rotate Vertical") > 0 && Input.GetAxis("Rotate Vertical") > targetTorque.x)
         { targetTorque.x = speed; }
         if (Input.GetAxis("Rotate Vertical") < 0 && Input.GetAxis("Rotate Vertical") < targetTorque.x)
         { targetTorque.x = -speed; }
 
         if (Input.GetAxis("Rotate Horizontal") > 0 && Input.GetAxis("Rotate Horizontal")  > targetTorque.y)
         { targetTorque.y = speed; }
         if (Input.GetAxis("Rotate Horizontal") < 0 && Input.GetAxis("Rotate Horizontal")  < targetTorque.y)
         { targetTorque.y = -speed; }
         //Stop1
         if (Input.GetAxis("Jump") >0.5) {
             if (targetVelocity.x > 0) { targetVelocity.x -= speed / 4; if (targetVelocity.x < 0) targetVelocity.x = 0; }
             if (targetVelocity.x < 0) { targetVelocity.x += speed / 4; if (targetVelocity.x > 0) targetVelocity.x = 0; }
             if (targetVelocity.z > 0) { targetVelocity.z -= speed / 4; if (targetVelocity.z < 0) targetVelocity.z = 0; }
             if (targetVelocity.z < 0) { targetVelocity.z += speed / 4; if (targetVelocity.z > 0) targetVelocity.z = 0; }
             targetVelocity.y = 0;
 
             if (targetTorque.x > 0) { targetTorque.x -= Torquespeed / 4; if (targetTorque.x < 0) targetTorque.x = 0; }
             if (targetTorque.x < 0) { targetTorque.x += Torquespeed / 4; if (targetTorque.x > 0) targetTorque.x = 0; }
             if (targetTorque.y > 0) { targetTorque.y -= Torquespeed / 4; if (targetTorque.y < 0) targetTorque.y = 0; }
             if (targetTorque.y < 0) { targetTorque.y += Torquespeed / 4; if (targetTorque.y > 0) targetTorque.y = 0; }
             targetTorque.z = 0;
 
             //targetTorque.y = 0;
             //targetTorque.x = 0;
         }
         if (Input.GetAxis("Stop1") > 0.5)
         {        
             if (targetTorque.x > 0) { targetTorque.x -= Torquespeed; if (targetTorque.x < 0) targetTorque.x = 0; }
             if (targetTorque.x < 0) { targetTorque.x += Torquespeed; if (targetTorque.x > 0) targetTorque.x = 0; }
 
             if (targetTorque.y > 0) { targetTorque.y -= Torquespeed; if (targetTorque.y < 0) targetTorque.y = 0; }
             if (targetTorque.y < 0) { targetTorque.y += Torquespeed; if (targetTorque.y > 0) targetTorque.y = 0; }
             targetTorque.z = 0;
         }
         if (Input.GetAxis("Stop2") > 0.5)
         {
             if (targetVelocity.x > 0) { targetVelocity.x -= speed; if (targetVelocity.x < 0) targetVelocity.x = 0; }
             if (targetVelocity.x < 0) { targetVelocity.x += speed; if (targetVelocity.x > 0) targetVelocity.x = 0; }
 
             if (targetVelocity.z > 0) { targetVelocity.z -= speed; if (targetVelocity.z < 0) targetVelocity.z = 0; }
             if (targetVelocity.z < 0) { targetVelocity.z += speed; if (targetVelocity.z > 0) targetVelocity.z = 0; }
             targetVelocity.y = 0;
         }
 
         if (targetVelocity.x > maxVelocityChange) { targetVelocity.x = maxVelocityChange; }
         if (targetVelocity.x < -maxVelocityChange) { targetVelocity.x = -maxVelocityChange; }
         if (targetVelocity.y > maxVelocityChange) { targetVelocity.y = maxVelocityChange; }
         if (targetVelocity.y < -maxVelocityChange) { targetVelocity.y = -maxVelocityChange; }
         if (targetVelocity.z > maxVelocityChange) { targetVelocity.z = maxVelocityChange; }
         if (targetVelocity.z < -maxVelocityChange) { targetVelocity.z = -maxVelocityChange; }
 
         if (targetTorque.x > maxTorqueChange) { targetTorque.x = maxTorqueChange; }
         if (targetTorque.x < -maxTorqueChange) { targetTorque.x = -maxTorqueChange; }
         if (targetTorque.y > maxTorqueChange) { targetTorque.y = maxTorqueChange; }
         if (targetTorque.y < -maxTorqueChange) { targetTorque.y = -maxTorqueChange; }
         if (targetTorque.z > maxTorqueChange) { targetTorque.z = maxTorqueChange; }
         if (targetTorque.z < -maxTorqueChange) { targetTorque.z = -maxTorqueChange; }
 
         torque = rigidbody.angularVelocity;
         Vector3 ProsTargetTorque = transform.TransformDirection(targetTorque);
         ProsTargetTorque *= Torquespeed;
         Vector3 TorqueChange = (ProsTargetTorque - torque);
         TorqueChange.x = Mathf.Clamp(TorqueChange.x, -maxTorqueChange, maxTorqueChange);
         TorqueChange.y = Mathf.Clamp(TorqueChange.y, -maxTorqueChange, maxTorqueChange);
         TorqueChange.z = 0;
 
         rigidbody.velocity = transform.TransformDirection(targetVelocity);
         rigidbody.AddRelativeTorque(TorqueChange, ForceMode.VelocityChange);
 
         Vector3 speedTrack = transform.InverseTransformDirection(velocity);
         Speed.text = "Speed: " + speedTrack.x.ToString("F1") + "," + speedTrack.y.ToString("F1") + "," + speedTrack.z.ToString("F1");
         Location.text = "Location: " + transform.position.x.ToString("F1") + "," + transform.position.y.ToString("F1") + "," + transform.position.z.ToString("F1");
         //Torque.text = "Torque: " + TorqueChange.x.ToString("F1") + "," + TorqueChange.y.ToString("F1") + "," + TorqueChange.z.ToString("F1");
               
         grounded = false;
     }
 
     void OnCollisionStay()
     {
         grounded = true;
     }
 
 
 }
Comment
Add comment · 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

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

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

Moving a CharacterController Object together with a RigidBody Object 1 Answer

Distribute terrain in zones 3 Answers

Convert Rigidbody code to CharacterController usable 0 Answers

Collision on character controller (fps player) 0 Answers

Having problems with animation and character controller velocity not matching 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