- Home /
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;
}
}
Answer by jmorhart · Jul 29, 2015 at 11:38 PM
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.
Interesting, thanks jmorhart! I understand everything you did in the code. The only thing I don't understand, is why do we have to cache the $$anonymous$$onoBehaviour's Transform as well?
I'll update later whether or not this code helped my game.
You'll want to cache the Transform because accessing it through the Component.transform property is quite slow. In fact, that property has been deprecated as of Unity 5 and will throw an error during compilation.
Answer by Hexer · Jul 29, 2015 at 10:35 PM
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.
Thank you very much I'll try this and the suggestions right away! I'll get back to you soon.
Okay so I tried it, and it doesn't fulfill the purpose of the follow the Player and Player follow rotation, but that's okay! After all you said it probably won't increase my frames very much. However I have another script that also follows the Player's position, but ins$$anonymous$$d of being right on it , it moves towards the Player.
It looks like this:
public Transform player;
float speed = 5.0f;
void Start () {
StartCoroutine (Death ());
}
IEnumerator Death()
{
yield return new WaitForSeconds (5.0f);
this.gameObject.SetActive (false);
}
void Update () {
if (player)
transform.position = Vector3.$$anonymous$$oveTowards (transform.position, player.position, speed * Time.deltaTime);
}
Is this good or can it be made better? Thank you for your time and help!
There seems to be nothing wrong. Try to use the Unity Profiler. Here's a like explaining what it does. Basically a thing inside of Unity that monitors your preformance. http://docs.unity3d.com/$$anonymous$$anual/Profiler.html
In the profiler you can see which components affect your preformance the most.
Hmm then I wonder what else is causing the drop of frames...
As for the profiler, I believe you can only use that very useful tool for Unity Pro. Am I correct? I have Unity free.
No, Unity profiler is now available for everybody that owns Unity5. regardless of owning the personal version or pro version.
I would recommend upgrading your Unity program. There are alot of changes in Unity5 with the key difference that everything that the pro version has, is also available for the free version ($$anonymous$$us some small details like custom splash screen etc)
Answer by Animatick · Jul 30, 2015 at 03:37 AM
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;
}
}
Your answer
Follow this Question
Related Questions
Following another object's position/rotation like parent/child relationship? 4 Answers
Flip over an object (smooth transition) 3 Answers
How to make Camera position Independent of its Rotation? 1 Answer
How can I have 2 transforms have the same position and rotation 0 Answers
Position and rotate 3 sprites perpendicular to the edges of a triangle 1 Answer