- Home /
Inefficient transform code.
I've developed this code to constantly place the object between the guardedObject and player at a specific distance. It's functional, however doing so makes the game very, very slow. Is there a more efficient way to implement this?
void Update () {
Vector3 NewPosition = -(guardedObject.transform.position - player.transform.position);
transform.localPosition = NewPosition.normalized * guardDistance;
}
EDIT: To clarify, guardDistance is a pre-determined float, currently set at 4. I made it a variable so I could adjust the distance if needed. Basically I want this object to always be 4 units away from Guarded Object, directly between it and the Player.
As @Berenger noted, one or two square roots shouldn't be able to cause your game being "very, very slow". Try the suggestions in both answers and see if that changes anything. Then comment out all three variants to make sure it is truly this code segment that's causing your problems.
Wolfram's answer below won't work, as lerping will cause the object to move further away from the guardedObject when the player moves away, when I want it to stay the same distance constantly.
I did discover something, though. Berenger's solution worked when sqrt_of_guardDistance was 2. The object would track the player and the game remained performant, but it would stay very close to the guardedObject. Increasing sqrt_of_guardDistance to 6 still had the object stay very close, but now the entire game was much slower.
Perhaps the lag is caused by trying to adjust the object to a very precise location with a high number of decimal places?
A high number of decimal places shouldn't matter considering float and double values cut off their decimal places past a certain point.
Unless you're running on extremely slow hardware (as in, older hardware than Unity even supports), there's no way this would cause any noticeable lag.
You wouldn't be using print or Debug.Log each frames on that script would you ? That would kick your computer into the precious.
Answer by Berenger · Jun 19, 2012 at 01:10 AM
Unless you've got hundreds instances of that script, this won't change much, but you could calculate the sqrt of guardDistance once at start and then use
transform.localPosition = (NewPosition / NewPosition.sqrMagnitude) * sqrt_of_guardDistance;
That way, you can save a sqrt per frame. You could calculate the direction, the magnitude and the yourself and ignore one component (y), if the game is 2D, so you can save some calculation too. That's hardly gonna change anything but it's the best I can think of. Your problem is probably elsewhere.*
Answer by Wolfram · Jun 19, 2012 at 01:06 AM
.normalized uses a square root, which might be what's slowing your game down. I don't know how you compute guardDistance (probably also using .magnitude or Vector3.Distance(), which both use square roots, too), but you could use this approach instead:
trnasform.position=Mathf.Lerp(player.transform.position, guardedObject.transform.position, factor);
If factor
is 0, object will be placed at player position, if it is 1, it will be placed at guardedObject position, and for values between that it will be...well, between them.