I have made a simple script wich rotates an object on a key press.
But I want to gently increase the speed to an X amount and when a user lets go of the key it gently decreases to 0.
I’m not sure on how to that, so any help is much appreciated
The script :
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Swing : MonoBehaviour
{
void Start()
{
}
void Update()
{
if (Input.GetKey("w")) transform.RotateAround(transform.position, transform.forward, Time.deltaTime * 10f);
if (Input.GetKey("s")) transform.RotateAround(transform.position, transform.forward, Time.deltaTime * -10f);
}
}
The W key rotates the object a certain way and the S key rotates the object a certain way.
public float rot_speed = 100;
public float rot_speedUp = 3;
Quaternion targetRotation;
void Start()
{
targetRotation = transform.rotation;
}
void Update()
{
float delta = Time.deltaTime;
if (Input.GetKey("w"))
targetRotation.eulerAngles += new Vector3(0, 0, rot_speed * delta); // Choose your axis, I chose z
else if (Input.GetKey("s")) // Prevents bugging in any way when both keys would be pressed
targetRotation.eulerAngles -= new Vector3(0, 0, rot_speed * delta); // Choose your axis, I chose z
transform.rotation = Quaternion.Lerp(transform.rotation, targetRotation, delta * rot_speedUp); // Applying the rotation
}
you can use a secondary number that keeps track of the time that the keys where pressed down and then multiply your speed by the new number
public float downtime;
void Update()
{
bool down = false;
if (Input.GetKey("w")){ down = true;transform.RotateAround(transform.position, transform.forward, downtime*Time.deltaTime * 10f);}
if (Input.GetKey("s")){ down = true; transform.RotateAround(transform.position, transform.forward, downtime*Time.deltaTime * -10f);}
if (down) {if(downtime<1){downtime+=Time.deltaTime;}}else{downtime = 0;}
}