transform.rotation = Quaternion.LookRotation(lastRotationPlayer);//makes the player look in the direction it was going when stopped.
That line of code is absolutely murdering my performance. I’m looking at my profiler and I’m getting upwards of 30-32 ms just to run that line of code. Even my A* scripts don’t run that slow… Once I remove the line, it goes back down to essentially 0 ms.
Why is that line of code so performance intensive and is there a way to do the same thing without using that function?
Are you running it on hundreds of scripts, or is it generating error messages? Because otherwise there’s nothing performance-intensive about it. You could speed it up a little by caching the transform reference.
–Eric
What value is getting assigned to lastRotationPlayer, and where is it defined?
if (Input.touchCount > 0)
{
if (ptrMoveStick.position.x != 0 || ptrMoveStick.position.y != 0)//reads if move stick isn't stationary.
{
transform.rotation = Quaternion.LookRotation(new Vector3(-ptrMoveStick.position.x, 0, -ptrMoveStick.position.y));//this rotates player if right stick isn't used.
normalizer = new Vector3(-ptrMoveStick.position.x, 0, -ptrMoveStick.position.y); //this is so player doesn't go back to vector3.zero;
normalizer.Normalize();
lastRotationPlayer = normalizer;
}
if (ptrRotateStick.position.x != 0 || ptrRotateStick.position.y != 0)//reads if rotate stick isn't stationary.
{
transform.rotation = Quaternion.LookRotation(new Vector3(-ptrRotateStick.position.x, 0, -ptrRotateStick.position.y));//this overrides left stick for rotate.
normalizer = new Vector3(-ptrRotateStick.position.x, 0, -ptrRotateStick.position.y); //this is so player doesn't go back to vector3.zero;
normalizer.Normalize();
lastRotationPlayer = normalizer;
}
}
that is where lastrotationplayer gets assigned. It is only running on the Player script, which is only attached to the one Human player.
I need it to update every frame so when the player stops moving, he looks the direction he was moving before. Otherwise when the player stops, their rotation jumps to something else (usually Vector3.zero).
oh and I’m not getting any errors or warnings.