Consider caching Transform changes

The Transform Component only stores data relative to its own parent. This means that accessing and modifying a Transform Component's position, rotation, and scale properties can result in a lot of unanticipated matrix multiplication calculations to generate the correct Transform representation for the object through its parents' Transforms. The deeper the object is in the Hierarchy, the more calculations are needed to determine the final result. To make matters worse, changes to a Transform Component also send internal notifications to colliders, rigid bodies, lights, and cameras, which must be processed.

However, this also means that using localPosition, localRotation, and localScale have a relatively trivial cost associated with them, because the values can be retrieved and written as they are passed in. Therefore, these local property values should be used whenever possible. However, changing our mathematical calculations from world space to local space can overcomplicate what were originally simple (and solved!) problems, so making such changes risks breaking our implementation and introducing a lot of unexpected bugs. Sometimes it's worth absorbing a minor performance hit in order to solve a complex 3D mathematical problem more easily!

In addition, it is not uncommon, during some complex event, we replace a Transform's properties multiple times in the same frame (although this is probably a warning sign of over-engineered design). We can consider minimizing the number of times we modify the Transform values by caching them in a member variable and committing them only at the end of the frame, as follows:

private bool _positionChanged;
private Vector3 _newPosition;

public void SetPosition(Vector3 position) {
  _newPosition = position;
  _positionChanged = true;
}

void FixedUpdate() {
  if (_positionChanged) {
    transform.position = _newPosition;
    _positionChanged = false;
  }
}

This code will only commit changes to the position in the next FixedUpdate() method.

Note that this will not result in sluggish-looking behavior during gameplay, since all physics calculations are performed immediately after FixedUpdate(). There would not be any frames rendered before the physics engine gets a chance to respond to the Transform change.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.221.50.185