- Home /
Door script lag with rotation (Quaternion.slerp ?)
I'm having some trouble using this slightly modified door script (javascript) I found off of Youtube. It works fine, but the problem is that I get a huge amount of lag from it. I've isolated which lines are causing the lag, and it is the Quaternion.slerp operations, and I have no idea why.
I have experience with coding but not with javascript in particular, nor am I familiar with the transform options. Can anyone point out why this script lags? I imagine because it is grabbing information every update when it doesn't necessarily need to, and that I could write a "sleep" function that skips any transform operations after the door has completed opening or closing, but that still means I'll get a bit of heavy lag for the few seconds while the door is swinging.
Any thoughts?
Thank you very much!
// Smothly open a door
var smooth = 2.0;
var DoorOpenAngle = 90.0;
var DoorCloseAngle = 0.0;
var open : boolean;
var enter : boolean;
var myTransform;
function Awake() {
myTransform = transform;
}
//Main function
function Update (){
if(open == true){
var target = Quaternion.Euler (0, DoorOpenAngle, 0);
// Dampen towards the target rotation
myTransform.localRotation = Quaternion.Slerp(myTransform.localRotation, target, Time.deltaTime * smooth);
}
if(open == false){
var target1 = Quaternion.Euler (0, DoorCloseAngle, 0);
// Dampen towards the target rotation
myTransform.localRotation = Quaternion.Slerp(myTransform.localRotation, target1, Time.deltaTime * smooth);
}
if(enter == true){
if(Input.GetKeyDown("f")){
open = !open;
}
}
}
//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;
}
}
Try using Lerp ins$$anonymous$$d of Slerp?
http://answers.unity3d.com/questions/299319/vector3slerp-problem.html
I looked up the difference and simply swapped the operations out, so now they are both Lerp. This does help a little... but I still suffer an enormous frame rate hit. Without the script I get a s$$anonymous$$dy 60 fps. With it I only get 13.
If you disable the collider on the door does it increase the performance?
Sadly that changes nothing. I am fairly confident the lag is co$$anonymous$$g from that Quaternion.slerp (or lerp) call, but I have no clue why it should. Surely this must be a commonly used line?
I tried your script in an empty scene attached to a cube and there was no noticeable change in frame rate, so maybe it is the door object itself which is causing the issue? Try attaching the script to a standard cube and see if you get the same results.
Your answer
Follow this Question
Related Questions
Door opening with a key, my scripts and problem 1 Answer
my script not working..!!! 1 Answer
Script for a door that requires (9 papers) to open 0 Answers
Open/close door, lags if I walk into it. 0 Answers
How to make 2 doors with 2 diffrent keys 2 Answers