- Home /
Unity - Rotation Stutter Problem
I am using simple scripts to open a car door/hood/boot as a personal project. I have the opening of the body part corresponding to a certain button (A button on Xbox controller or CTRL on keyboard).
Sometimes when I press the button to open the door, it does not complete rotation but only stutters. This happens with the Xbox controller and the keyboard button (so I don't think it's an isolated button issue). My thinking is that it could be an issue with the button being read as being pressed twice so it may need to be debounced? But even with the quickest click of a button the error still occurs.
Any help is appreciated and any suggestions are welcome!
VIDEO: Here is a video demo of the stutter problem which might help make what I'm asking clearer: https://www.youtube.com/watch?v=Ic8LyB2lvLk
CODE (JS):
#pragma strict
var smooth = 2;
var DoorOpenAngle = 70.0;
private var open : boolean;
private var enter : boolean;
private var defaultRot : Vector3;
private var openRot : Vector3;
private var closedRot : Vector3;
private var v3Current : Vector3;
function Start(){
defaultRot = transform.eulerAngles;
openRot = new Vector3(defaultRot.x, defaultRot.y + DoorOpenAngle, defaultRot.z);
closedRot = new Vector3(defaultRot.x, defaultRot.y, defaultRot.z);
v3Current = new Vector3(defaultRot.x, defaultRot.y, defaultRot.z);
open = false;
}
//Main function
function Update (){
if(Input.GetButton("Fire1") && enter){
open = !open;
}
if(open)
{
//transform.eulerAngles = Vector3.Slerp(transform.eulerAngles, openRot, Time.deltaTime * smooth);
v3Current = Vector3.Slerp(v3Current, openRot, Time.deltaTime); // Time.deltaTime * smooth
transform.eulerAngles = v3Current;
}
else
{
v3Current = Vector3.Slerp(v3Current, closedRot, Time.deltaTime);
transform.eulerAngles = v3Current;
}
/*if(Input.GetKeyDown("f") && enter){
open = !open;
} */
}
function OnGUI(){
if(enter){
GUI.Label(new Rect(Screen.width/2 - 75, Screen.height - 100, 150, 50), "To open/close DOOR, press A.");
}
}
//Activate the Main function when player is near the door
function OnTriggerEnter (other : Collider){
if (other.gameObject.tag == "Player") {
enter = true;
}
}
//Deactivate the Main function when player is go away from door
function OnTriggerExit (other : Collider){
if (other.gameObject.tag == "Player") {
enter = false;
}
}