- Home /
Question by
gb3803 · Dec 28, 2013 at 02:14 AM ·
javascriptscripting problemmonodevelop
how to make it that longer the key is pressed the faster the object rotates?
this is my script
#pragma strict
var rotationSpeed = 100;
var jumpHeight = 8;
private var isFalling = false;
function Update ()
{
// handle ball rotation.
var rotation : float = Input.GetAxis ("Horizontal") * rotationSpeed;
rotation *= Time.deltaTime;
rigidbody.AddRelativeTorque (Vector3.back * rotation);
if (Input.GetKeyDown (KeyCode.Space))
{
rigidbody.velocity.y = jumpHeight;
}
}
Comment
I would increment some number while the button is pressed and use that for the speed:
private var rot_speed = 0.0;
function Update(){
var h : float = Input.GetAxis("Horizontal");
if(h != 0){
//$$anonymous$$eep the value positive and increase
h = $$anonymous$$athf.Abs(h);
rot_speed += h * Time.deltaTime);
}
else{
//If nothing is pressed, decrease the speed and keep above 0
rot_speed = $$anonymous$$athf.$$anonymous$$ax(rot_speed - Time.deltaTime, 0.0);
}
var rotation : float = rot_speed * Time.deltaTime;
rigidbody.AddRelativeTorque(...);
}
This way will only rotate in one direction if you press left or right, but it can be changed to go both directions
Answer by Jeff-Kesselman · Dec 28, 2013 at 03:35 AM
You can use an axis, that will scale as you hold the key,, between 0 and 1.
Otherwise as others have said, keep a speed variable and increment it by some constant times the elapsed frame time every Update that the key is pressed.