- Home /
Other
Rotation Speed
How can I set how fast a ship turns with rotation? say maybe degrees per sec or something? Heres the current code…
using UnityEngine;
using System.Collections;
public class ShipControl : MonoBehaviour
{
public Transform ship;
float flyingSpeed = 0;
float turnSpeed = 50.0f;
void Update ()
{
//Accelerate the ship using the thrust key.
if(Input.GetKeyDown("e"))
{
if(flyingSpeed == 0)
{
flyingSpeed = 2.5f;
}
else if(flyingSpeed == 2.5f)
{
flyingSpeed = 5f;
}
else if(flyingSpeed == 5f)
{
flyingSpeed = 7.5f;
}
else if(flyingSpeed == 7.5f)
{
flyingSpeed = 10f;
}
}
//Decelerate the ship using the thrust button.
if(Input.GetKeyDown("q"))
{
if(flyingSpeed == 10f)
{
flyingSpeed = 7.5f;
}
else if(flyingSpeed == 7.5f)
{
flyingSpeed = 5f;
}
else if(flyingSpeed == 5f)
{
flyingSpeed = 2.5f;
}
else if(flyingSpeed == 2.5f)
{
flyingSpeed = 0;
}
else
{
flyingSpeed = 0;
}
}
if(Input.GetKey("w"))
{
ship.transform.Rotate(20.0f * Time.deltaTime, 0.0f, 0.0f);
}
if(Input.GetKey("s"))
{
ship.transform.Rotate(-20.0f * Time.deltaTime, 0.0f, 0.0f);
}
if(Input.GetKey("d"))
{
ship.transform.Rotate(0.0f, turnSpeed * Time.deltaTime, 0.0f * Time.deltaTime, Space.World); //and then turn the plane
}
if(Input.GetKey("a"))
{
ship.transform.Rotate(0.0f, -turnSpeed * Time.deltaTime, 0.0f * Time.deltaTime, Space.World); //and then turn the plane
}
ship.transform.Translate(0,-flyingSpeed * Time.deltaTime,0);//move the ship
}
}
Not sure what you are asking. 'turnSpeed * Time.deltaTime' in your Rotate is degrees per second. If you are looking for a total for all axes, create a Vector3 containing all three turns and look at the magnitude of the vector3. Note since 'turnSpeed' is public, the value must be set in the inspector after the script is added.
it says lol "How can I set how fast a ship turns" meaning making a set turn speed...
Again, it is unclear what you are asking. Your code uses 'turnSpeed', and, because you use 'deltaTime', your calculation are already in degrees per second. To make a change, select your game object with the above script in the Hierarchy view, and in the inspector change the value of 'turnSpeed'.
I have turn speed but don't under stand what value it holds...
The Rotate function takes in an angle (Euler). If you apply that over time you get degrees per second. Ex: turnspeed = 10 so turnspeed * Time.delta time will add up to 10 over the course of 1 second.
Answer by insominx · Dec 13, 2013 at 11:17 PM
Here's an example:
public class SpinningBox : MonoBehaviour {
public float yawDegreesPerSecond = 10.0f;
// Update is called once per frame
void Update () {
Vector3 localEulers = transform.localEulerAngles;
localEulers.y += yawDegreesPerSecond * Time.deltaTime;
transform.localEulerAngles = localEulers;
}
}
Follow this Question
Related Questions
Flip over an object (smooth transition) 3 Answers
Images Rotation 1 Answer
Space Torpedo 1 Answer
C# - How to vary rotation speed according to movement speed 1 Answer
Distribute terrain in zones 3 Answers