An Efficient Way To Follow Transform's Position?

Unity Version 4.3.4, C# scripting. Barely learned Unity last year. Wish I had Pro to solve my problems. Wink wink? Hint?

Hi again fellow Unity game developers. How are you? Good? Well I’m not! You see I’ve spent the last couple of days trying to optimize my game for IOS that lags on certain parts which screws up gameplay, but I’ve been having quite a hard time. So I’ve been making some decent progress with object pooling, eliminating empty voids, and dynamic batching, but I need help with this one thing.

I have an empty game object with the script below attached to it. It follows the Player’s position, and I also have a different script attached to the Player that follows the empty game object’s rotation. Now I’ve looked up many things on how to optimize the scripts so that they won’t make my game’s frame drops worse, but alas I could not find anything that helped me. I’ve read about caching but I could not get it to work because I’m a little stupid(I admit it).
So I ask of anyone to please help me find an efficient way for this script! I know this and other scripts similar to this are part of the last remaining things that drop my frames. So please if anyone could help, help!

public Transform track;
	
	// Update is called once per frame
	void Update () {
		
		if (track)
			transform.position = track.position;

		}
}

Other script on Player:
public Transform track;

	// Update is called once per frame
	void Update () {
		transform.rotation = track.rotation;
	
	}
}

You could set it in a function of its own and InvokeRepeat it.

void Start(){
InvokeRepeating("TrackPlayer",0.1f,30.0f);
}

void TrackPlayer(){
transform.rotation = track.rotation;
}

This will Invoke the function TrackPLayer and call the transform.rotation 30x a second instead of 60x like void Update does.
I highly doubt this will increase your Frames by more than 5…

If you got Resources.Load(),Debug.Log(),print() or gameObject.Find() in your scripts. Get rid of them (re-define them). I once removed a line with Resources.Load() and redefined it as gameObject.FindWithTag() and it increased my Frames double fold.

hello i don’t know if this helps or not but this script is attached to a camera and will follow your player from wherever the position of the camera is… just attach it to the camera you want to follow your player then drag your player to the slot in the inspector

using UnityEngine;
using System.Collections;

public class CameraController : MonoBehaviour {
	
	public GameObject player;
	
	private Vector3 offset;
	
	void Start ()
	{
		offset = transform.position - player.transform.position;
	}
	
	void LateUpdate ()
	{
		transform.position = player.transform.position + offset;
	}
}

You probably want to cache the position and only update if the position has changed. Something like this:

public Transform track;

private Transform cachedTransform;
private Vector3 cachedPosition;

void Start()
{
   cachedTransform = GetComponent<Transform>();
   if (track)
   {
      cachedPosition = track.position;
   }
}

void Update()
{
   if (track && cachedPosition != track.position)
   {
      cachedPosition = track.position;
      transform.position = cachedPosition;
   }
}

As a side note to Hexer’s answer, Update() does get called 60 times per second. Update is called as often as possible, dependent on the frame rate. You’re probably thinking of FixedUpdate(), which is called every time Unity’s physics system updates (which is also not necessarily 60 times per second).

Finally, you would also want to cache the MonoBehaviour’s Transform as well, which I have demonstrated in the code above.