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);
}
}
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;
}
}
}