# Limited speed of Roll Angle?

Discussion in 'Scripting' started by cruising, May 17, 2015.

1. ### cruising

Joined:
Nov 22, 2013
Posts:
325
Hello!

Working on a movement script, my problem is that i fail to limit the speed of the rolling angle depending on forward speed, zero speed should not roll at all and max speed should do minimum roll.

And also it rolls in the wrong direction even if the Input in unity aint inverted, if i invert it roll in the right direction, how come?

Here is the code:
Code (CSharp):
1.     public float Speed  = 50.0f;
2.     public float maxSpeed = 100.0f;
3.     public float minSpeed = 0.0f;
4.     public float maxAcceleration = 30.0f;
5.     public float currentSpeed = 0.0f;
6.     public float maxRollAngle = 30;
7.
8.     float currentAngle = 0;
9.
10.     void Update()
11.     {
12.         float targetAngle = Input.GetAxis("Horizontal") * maxRollAngle;
13.         float Speed = 1 + (Mathf.Abs(Input.GetAxis("Horizontal") * 10));
14.
15.         transform.Translate(Vector3.forward * Time.deltaTime * currentSpeed);
16.         currentAngle = Mathf.Lerp(currentAngle, targetAngle, Speed);
17.         transform.rotation = Quaternion.Euler(0, 0, currentAngle);
18.
19.
20.
21.         if(Input.GetKey("e"))
22.             currentSpeed += maxAcceleration * Time.deltaTime;
23.
24.         else if(Input.GetKey("q"))
25.             currentSpeed-=maxAcceleration * Time.deltaTime;
26.
27.         else if (currentSpeed > Speed){
28.             currentSpeed = currentSpeed;
29.         }
30.         else if (currentSpeed < Speed){
31.             currentSpeed = currentSpeed;
32.         }
33.         else {
34.             currentSpeed += maxAcceleration * Time.deltaTime;
35.             currentSpeed = Mathf.Clamp(currentSpeed, minSpeed, Speed);
36.         }
37.         currentSpeed = Mathf.Clamp(currentSpeed, minSpeed, maxSpeed);
38.     }
39. }

Last edited: May 18, 2015
2. ### calmcarrots

Joined:
Mar 7, 2014
Posts:
681
Oh my goodness, please organize your code. Such an eyesore, I dont even wanna look at it

3. ### cruising

Joined:
Nov 22, 2013
Posts:
325
Is it better now? Cant understand why that matters for a short script like this?, if you dont wanna help you simply dont wanna help 4. ### DustySkunk

Joined:
Sep 12, 2013
Posts:
13
It would help me immensely if you could describe exactly what you are looking to do with this script. What are you trying to simulate?

Two quick things right off the bat:

1) When you run this script: if you print out the values of currentangle and targetangle to the console, are they roughly what you expect them to be? I also noticed you are using Speed and not currentspeed when you assign currentangle. Is that a typo or on purpose?

2) This entire section seems problematic to me:
So right now, you have a case where if Speed is exactly equal to currentspeed and neither q nor e are pressed, your object is set to accelerate, but it will only do that for a one or two update cycles at most until currentspeed is greater or less than Speed... is that your desired behavior?

I know your question was about the roll angle but if we have more information regarding what it is you're trying to accomplish we will be able to assist you more readily.

5. ### cruising

Joined:
Nov 22, 2013
Posts:
325

Im trying to make some space ship movements. with Q and E as trottle, when you turn the ship right or left it makes a 30-45 degrees roll, abillity to rotate while zero trottle with no roll.

If they are roughly what i expect them to be?, im not sure what you exactly mean about that. debug.log?

About the "Speed" if i have "CurrentSpeed" the ship will not accelerate at all, so for how the code looks like now i need "Speed" in that line, I guess this code is pretty worthless done since the ship kind of "lags" forward..guess it is the update?

About the Q and E, thats the trottle as you know and as long as you press Q it will lower the speed unitll zero, and increase with E untill max, if you stop puch the button before min and max it clamp the current speed.

I hope my half stupid answears answear you some how better then my first post.

EDIT: the ship dont lag anymore

EDIT2: i have removed some stuffs that wasnt needed, the ships acts the same with this code

Code (CSharp):
1. using UnityEngine;
2. using System.Collections;
3.
4. public class HoverStabilizer : MonoBehaviour {
5.
6.     public float Speed  = 50.0f;
7.     public float maxSpeed = 100.0f;
8.     public float minSpeed = 0.0f;
9.     public float maxAcceleration = 30.0f;
10.     public float currentSpeed = 0.0f;
11.     public float maxRollAngle = 30;
12.
13.     float currentAngle = 0;
14.
15.     void Update()
16.     {
17.         float targetAngle = Input.GetAxis("Horizontal") * maxRollAngle;
18.         float Speed = 1 + (Mathf.Abs(Input.GetAxis("Horizontal") * 10));
19.
20.
21.         transform.Translate(Vector3.forward * Time.deltaTime * currentSpeed);
22.         currentAngle = Mathf.Lerp(currentAngle, targetAngle, Speed);
23.         transform.rotation = Quaternion.Euler(0, 0, currentAngle);
24.
25.
26.
27.         if(Input.GetKey("e"))
28.             currentSpeed += maxAcceleration * Time.deltaTime;
29.
30.         else if(Input.GetKey("q"))
31.             currentSpeed-=maxAcceleration * Time.deltaTime;
32.
33.         else if (currentSpeed < Speed){
34.             currentSpeed = currentSpeed;
35.         }
36.         currentSpeed = Mathf.Clamp(currentSpeed, minSpeed, maxSpeed);
37.     }
38. }

Last edited: May 18, 2015
6. ### Korno

Joined:
Oct 26, 2014
Posts:
515
The last parameter of Lerp needs to be a value between 0-1 right? Is speed between 0-1? unityunity