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 Naszia · Sep 01, 2011 at 08:13 AM · carwheel

Car wheel script delay.

I am using this script for my car and for some reason that i really cannot figure out, there is a quite substantial delay from the time i hit the arrow buttons to when the car actually steers. Please help.

Script:

 var rearWheel1 : WheelCollider;
 var rearWheel2 : WheelCollider;
 var frontWheel1 : WheelCollider;
 var frontWheel2 : WheelCollider;
  
 var wheelFL : Transform;
 var wheelFR : Transform;
 var wheelRL : Transform;
 var wheelRR : Transform;
  
 var steer_max = 20;
 var motor_max = 40;
 var brake_max = 100;
 var steerSpeed = 20;
  
 private var steer = 0;
 private var forward = 0;
 private var back = 0;
 private var brakeRelease = false;
 private var motor = 0;
 private var brake = 0;
 private var reverse = false;
 private var speed = 0;
  
 function Start() {
   print(steer); //Here is where is put it             *************
 rigidbody.centerOfMass = Vector3(0, -0.05, 0);
 }
  
 function FixedUpdate () {
  
 speed = rigidbody.velocity.sqrMagnitude;
 steer = Input.GetAxis("Horizontal");
 forward = Mathf.Clamp(Input.GetAxis("Vertical"), 0, 1);
 back = -1 * Mathf.Clamp(Input.GetAxis("Vertical"), -1, 0);
  
 if(speed == 0 && forward == 0 && back == 0) {
 brakeRelease = true;
 }
  
 if(speed == 0 && brakeRelease) {
 if(back > 0) { reverse = true; }
 if(forward > 0) { reverse = false; }
 }
  
 if(reverse) {
 motor = -1 * back;
 brake = forward;
 } else {
 motor = forward;
 brake = back;
 }
 if (brake > 0 ) { brakeRelease = false; };
  
 rearWheel1.motorTorque = motor_max * motor;
 rearWheel2.motorTorque = motor_max * motor;
 rearWheel1.brakeTorque = brake_max * brake;
 rearWheel2.brakeTorque = brake_max * brake;
  
 if ( steer == 0 && frontWheel1.steerAngle != 0) {
 if (Mathf.Abs(frontWheel1.steerAngle) <= (steerSpeed * Time.deltaTime)) {
 frontWheel1.steerAngle = 0;
 } else if (frontWheel1.steerAngle > 0) {
 frontWheel1.steerAngle = frontWheel1.steerAngle - (steerSpeed * Time.deltaTime);
 } else {
 frontWheel1.steerAngle = frontWheel1.steerAngle + (steerSpeed * Time.deltaTime);
 }
 } else {
 frontWheel1.steerAngle = frontWheel1.steerAngle + (steer * steerSpeed * Time.deltaTime);
 if (frontWheel1.steerAngle > steer_max) { frontWheel1.steerAngle = steer_max; }
 if (frontWheel1.steerAngle < -1 * steer_max) { frontWheel1.steerAngle = -1 * steer_max; }
 }
 frontWheel2.steerAngle = frontWheel1.steerAngle;
 wheelFL.localEulerAngles.y = frontWheel1.steerAngle;
 wheelFR.localEulerAngles.y = frontWheel2.steerAngle;
  
 wheelFR.Rotate(frontWheel1.rpm * 5 * Time.deltaTime, 0,0 );
 wheelFL.Rotate(frontWheel2.rpm * 5 * Time.deltaTime, 0,0 );
Comment
Add comment · Show 14
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 CHPedersen · Sep 01, 2011 at 09:51 AM 0
Share

No one is willing to read a mad jumble of code. Format your code properly using the "101010"-button, or no one will help you.

avatar image SisterKy · Sep 01, 2011 at 03:02 PM 0
Share

Do not post the same question again (with worse formatting), but hit the 'edit' button at the lower left of your post, then select all your code and hit the 101010-button as christian is telling you.

avatar image Naszia · Sep 02, 2011 at 05:15 AM 0
Share

Sorry guys, i couldent see the edit button cause i kept on getting logged out. Anyways figured it out. Please help.

avatar image CHPedersen · Sep 02, 2011 at 07:17 AM 0
Share

It looks to me like it really should begin steering at once, since already at the first call to your FixedUpdate, it begins incrementing the steerAngle of both wheels by the product of the steer variable and the steer speed.

Not knowing the relative values of steer and steerAngle, it's hard to deter$$anonymous$$e just from looking at the code what it is that goes wrong, but I have a couple of "shot in the dark"-suggestions. First, print out steer to the console along with the steerAngle after the frontWheel1 has been incremented. Do they acquire values that you expect? Secondly, might some values be integers, when you actually expect them to be floats? Perhaps the delay you're seeing is happening because the steerAngle gets calculated to some value between 0 and 1 for a while to begin with, but gets truncated to 0 because the system thinks it's an integer? If that is the case, then the steering would seem "jerky" because the angle always has discrete, integer values assigned to it.

All in all:

Try to explain in greater detail how the steering behaves. What happens if you press and hold one arrow key for a while? How does it behave when you release the key? Does it correct itself properly?

avatar image Naszia · Sep 02, 2011 at 07:23 AM 0
Share

Thank you very much, ill be looking into what you said. and i have put it on a server for you guys to play so you can get a first can see what the problem is:

You will of course need the unity plug in raws.adc.rmit.edu.au/~s3286249

Show more comments

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by CHPedersen · Sep 02, 2011 at 11:15 AM

It appears so, yes. I just tested it, too. So basically, you have a variable, steer, which is a float that goes from -1 to 1, and seems to lerp back to 0 when you release the key. Then you have another variable, steerSpeed, which is set to 20, and then you have Time.deltaTime, which is always 0.02, which is probably 0.016666... rounded up, because the FixedUpdate runs 60 times per second. Let's imagine you're turning left, i.e. steer runs from 0 to -1. The first value I get is -0.02985108, and on the second call to FixedUpdate, it is -0.07792716, and so on.

So, the first time your FixedUpdate runs, it sets frontWheel1.steerAngle to -0.02985108 20 0.02 = -0.011940432. That's a very, very small angle.

Then, next time it runs, it subtracts -0.07792716 20 0.02 = -0.031170864, making the total angle to turn the wheels by equal -0.011940432 + -0.031170864 = -0.043111296. That's also a small angle, and by now, 2/60 of a second has passed.

I ran this calculation myself and printed stuff to the console while I was at it with these simple few lines of code:

 float val = 0;
 int frames = 0;
 
 void FixedUpdate()
 {
     val += Input.GetAxis("Horizontal") * 20 * Time.deltaTime;
     frames++;
     Debug.Log("Value: " + val + ", Frames: " + frames);
 }

It took 34 frames (34 calls to FixedUpdate) just to get the value to -10.3. This means it took a little over half a second just to save up an angle of 10 degrees, and it took 57 calls to FixedUpdate before the angle became 20 (the value of max_steer), which means you have to press and hold the left arrow key for almost a full second, before the wheels are turned as far to the left as they will go.

So, there you have it. The reason you have a delay in turning your wheels is simply that the angle with which you turn the wheels adds up too slowly, particularly in the beginning. To fix it, increase the value of steerSpeed, or use a non-linear function that turns quickly in the beginning and slower towards the end, such a logarithmic function, for instance.

Comment
Add comment · Show 6 · 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 Naszia · Sep 02, 2011 at 12:03 PM 0
Share

Thank you very much Christian. Though incresing the steer speed does nothing to the delay. And i dont trust my self to write this logarithmic function. it sounds like hell. Sorry if im being a pain. But could you explain how i would go about writing it?

avatar image CHPedersen · Sep 02, 2011 at 12:18 PM 0
Share

Forget about that for now. Ins$$anonymous$$d, try to write a simple if-sentence, that sets steer to -1 or 1 immediately, depending on whether it is positive or negative to begin with, ins$$anonymous$$d of waiting for consecutive calls to FixedUpdate to wind it up to -1/1.

avatar image Naszia · Sep 02, 2011 at 12:39 PM 0
Share

Im seriously lost. Cant i just set the var to -1?

avatar image CHPedersen · Sep 02, 2011 at 12:46 PM 0
Share

Sure, that's what I said. :P But you don't want to set it to -1 all the time, since -1 is only when you're turning left. You want it to be +1 when you're turning right. So, in a frame where it is positive, set it to 1, and in a frame where it is negative, set it to -1.

avatar image Naszia · Sep 02, 2011 at 01:03 PM 0
Share

So say in one of the else statements to do with the steering angel?

Show more comments

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

car script / powering wheel colliders 1 Answer

Simple steering wheel rotation in a car 1 Answer

WheelCollider - real friction values 0 Answers

in Unity 3.5.7 car starts to shake 0 Answers

can't slow my car down 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