- Home /
Smoothly increase rotation speed when pressing a key
Hi,
I'm fairly new to unity and C#, so forgive me for this noob question.
I'm making a funfair ride, and I want to increase the rotation of an object when someone presses a key, for example "W" and "S" The code that I created now is simple, it goes to the speed I set, but it doesn't smoothly increase overtime while pressing the key.
Can someone help me with it?
Thanks!!
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Kruis : MonoBehaviour
{
void Start()
{
}
void Update()
{
if (Input.GetKey("w")) transform.RotateAround(transform.position, transform.up, Time.deltaTime * 210f);
if (Input.GetKey("s")) transform.RotateAround(transform.position, transform.up, Time.deltaTime * 0f);
}
}
Answer by Hellium · Aug 30, 2019 at 10:08 AM
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Kruis : MonoBehaviour
{
public float RotationSpeed = 1;
public float RotationAcceleration = 1;
public float MaxRotationSpeed = 50;
public KeyCode AccelerationKey = KeyCode.W;
public KeyCode DecelerationKey = KeyCode.S;
public Vector3 RotationAxis = Vector3.up;
public bool RotateAroundWorldAxis;
public bool RotateAntiClockWise;
private float currentRotationSpeed;
void Start()
{
currentRotationSpeed = RotationSpeed;
}
void Update()
{
if (Input.GetKey(AccelerationKey))
{
currentRotationSpeed += Time.deltaTime * RotationAcceleration;
}
if (Input.GetKey(DecelerationKey))
{
currentRotationSpeed -= Time.deltaTime * RotationAcceleration;
}
currentRotationSpeed = Mathf.Clamp(currentRotationSpeed, 0, MaxRotationSpeed);
float angle = currentRotationSpeed;
if (RotateAntiClockWise)
angle = -angle;
Vector3 rotationAxis = RotationAxis;
if (!RotateAroundWorldAxis)
rotationAxis = transform.rotation * RotationAxis;
transform.RotateAround(transform.position, rotationAxis, angle);
}
#if UNITY_EDITOR
private void OnDrawGizmosSelected()
{
Quaternion rotation = RotateAroundWorldAxis ? Quaternion.identity : transform.rotation;
Vector3 rotationAxis = rotation * RotationAxis;
float angle = RotateAntiClockWise ? 1/2f : 1;
float radius = 0.5f;
Color gizmosColor = Color.magenta;
Color handlesColor = UnityEditor.Handles.color;
Vector3 arrowCap = transform.position + rotationAxis + transform.rotation * new Vector3(Mathf.Cos(angle * Mathf.PI), 0, Mathf.Sin(angle * Mathf.PI)) * radius;
Vector3 leftArrowEnd = arrowCap + Quaternion.Euler(0, RotateAntiClockWise ? 0 : 90, 0) * rotation * new Vector3(0.1f, 0, 0.1f) ;
Vector3 rightArrowEnd = arrowCap + Quaternion.Euler(0, RotateAntiClockWise ? 0 : 90, 0) * rotation * new Vector3(0.1f, 0, -0.1f);
UnityEditor.Handles.color = gizmosColor;
Debug.DrawLine(transform.position - rotationAxis, transform.position + rotationAxis, gizmosColor);
UnityEditor.Handles.DrawWireArc(transform.position + rotationAxis, rotationAxis, transform.forward, 270, radius);
Debug.DrawLine(arrowCap, leftArrowEnd, gizmosColor);
Debug.DrawLine(arrowCap, rightArrowEnd, gizmosColor);
UnityEditor.Handles.color = handlesColor;
}
#endif
}
ORIGINAL ANSWER
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Kruis : MonoBehaviour
{
public float RotationSpeed = 1;
public float RotationAcceleration = 1;
private float currentRotationSpeed;
void Start()
{
currentRotationSpeed = RotationSpeed;
}
void Update()
{
if (Input.GetKeyDown("w") || Input.GetKeyDown("s"))
{
currentRotationSpeed = RotationSpeed;
}
if (Input.GetKey("w"))
{
transform.RotateAround(transform.position, transform.up, currentRotationSpeed);
currentRotationSpeed += Time.deltaTime * RotationAcceleration;
}
if (Input.GetKey("s"))
{
transform.RotateAround(transform.position, transform.up, currentRotationSpeed);
currentRotationSpeed += Time.deltaTime * RotationAcceleration;
}
}
}
Thanks! That totally works! Amazing. But it doesn't hold the speed.
I want "W" to go faster and "S" to slow down.
is that possible?
Do you want the object to rotate automatically, and rotate it faster and faster while W
is pressed, and slow it down when S
is pressed?
If so, the script must be different.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class $$anonymous$$ruis : $$anonymous$$onoBehaviour
{
public float RotationSpeed = 1;
public float RotationAcceleration = 1;
private float currentRotationSpeed;
void Start()
{
currentRotationSpeed = RotationSpeed;
}
void Update()
{
if (Input.Get$$anonymous$$ey("w"))
{
currentRotationSpeed += Time.deltaTime * RotationAcceleration;
}
if (Input.Get$$anonymous$$ey("s"))
{
currentRotationSpeed = $$anonymous$$athf.$$anonymous$$ax( currentRotationSpeed - Time.deltaTime * RotationAcceleration, 0 );
}
transform.RotateAround(transform.position, transform.up, currentRotationSpeed);
}
}
No, I want "W" to start the rotation and "S" to slow down and eventually stop the rotation.
Thanks for the help thus far! :)
I've added a new code to my answer which is pretty complete I believe. You can customize several things directly in the inspector.
Now I need one that rotates on its X Axis, but I'm not sure on how to that. Could you perhaps help me again?
Have you tried to attach the component twice, change the axis and the keys?
Your answer
Follow this Question
Related Questions
How to use Animator Override Controler in script? 0 Answers
Random generation 1 Answer
How to assign script to inspector variable 2 Answers
Unity 5 C#, having a script show up in a path in the Editor 1 Answer
Encrypting / Verifying script files 0 Answers