- Home /
Question by
Chocolade · Aug 09, 2017 at 09:24 PM ·
c#scripting problemscript.
How can i slow down object rotation when releasing the mouse button and speed up when pressing the button again ?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class RotateGuns : MonoBehaviour
{
public float rotationSpeed = 90;
private void Update()
{
if (Input.GetMouseButton(0))
{
// Rotate guns according to rate of fire bullets amount.
transform.Rotate(Vector3.right * Time.deltaTime * rotationSpeed);
}
}
}
This rotate the object in static speed.
But i want when i start pressing the mouse button it will slowly rotate and will speed up until max speed. And when releasing the mouse button at any time it will start slowly down rotating until min speed.
Comment
Best Answer
Answer by unidad2pete · Aug 09, 2017 at 10:08 PM
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class RotateGuns : MonoBehaviour
{
public float rotationSpeedMin = 90;
public float rotationSpeedMax = 180;
public float currentSpeed = 0;
public float acceleration = 1;
private void Update()
{
if (Input.GetMouseButton(0))
{
// Rotate guns according to rate of fire bullets amount.
if(currentSpeed < rotationSpeedMax)
{
currentSpeed += acceleration * Time.deltaTime;
}
transform.Rotate(Vector3.right * Time.deltaTime * currentSpeed);
} else
{
if(currentSpeed > rotationSpeedMin)
{
currentSpeed -= acceleration * Time.deltaTime;
}
transform.Rotate(Vector3.right * Time.deltaTime * currentSpeed);
}
}
}