How to increase and decrease rotating speed
I have speed data like 10 m/s, 6 m/s, 15 m/s, 20 m/s. I want to rotating speed increase by 10 m/s after time speed 6 m/s something like that variable speed. I already wrote rotating speed script code as follow:
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class RotateGO : MonoBehaviour { float speed = 1.0f;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
//transform.Rotate(Vector3.up * speed * Time.deltaTime);
transform.Rotate(speed, 0, 0);
speed += Time.deltaTime;
speed -= 0.4f;
}
}
Please help for write correct script file Thank you
Answer by Dizy · May 14, 2020 at 02:37 PM
Hi, there is no such m/s things about rotation but only degrees. If i understand you want to increase your rotation based on the elapsed time.
That should do the trick :
public float Speed;
private float _currentTime;
void Update() {
_currentTime += Time.deltaTime;
transform.Rotate(Speed * _currentTime, 0, 0)
}
You can also use rigidBody.AddTorque() if you want to simulate this using physics.
EDIT : to calculate your speed based on your time
For that you need to use a cross product with backing zero.
With MinTime, MaxTime, MinSpeed, MaxSpeed, _currentTime, _currentSpeed
_currentSpeed = MinSpeed + ((MaxTime - MinTime) * (_currentTime - MinTime) / (MaxSpeed - MinSpeed))
Not tested but it's seems to be okay.
You probably need to add some condition to check if the _currentTime is below MinTime
Yes I tried it. Object continuously rotated. I need range of time and speed. $$anonymous$$y mean how to write code for time of range (1-60) and speed of range (11-25). For example: Time 1 at speed 11 Time 2 at speed 15 ` Time 3 at speed 20 Please help me to write code
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class RotateGO : $$anonymous$$onoBehaviour {
public int speed;
public int time;
void Start()
{
}
void Update()
{
transform.Rotate(+speed * Time.deltaTime, 0, 0);
time = Random.Range(1, 5000);
speed = Random.Range(5, 25);
Debug.Log("time: " +time+",speed:"+speed);
if (time == 10)
{
transform.Rotate(speed * Time.deltaTime, 0, 0);
print("1000");
}
else
{
if (time == 1000)
{
transform.Rotate(speed + 10 * Time.deltaTime, 0, 0);
print("2000");
}
else
{
if (time == 3000)
{
transform.Rotate(speed + 20 * Time.deltaTime, 0, 0);
print("3000");
}
}
}
}
}
Answer by Oyunja · May 15, 2020 at 12:44 PM
This is my code. It's running well. But I want to change "time Random.Range" by the Time Loop. How to write and fix Time loop code in this script?
https://docs.unity3d.com/ScriptReference/Time.html
realtimeSinceStartup The real time in seconds since the game started (Read Only).
Use the Unity documentation, RTF$$anonymous$$ if i can say.
Your answer
Follow this Question
Related Questions
How i apply rotating animation via script ? 0 Answers
How to speed up player character in ENDLESS RUNNER after colliding with powerup?? 0 Answers
Rotating a grid, 0 Answers
accelerate speed 0 Answers