Hey! I’m fairly new to Unity and after reading through the API can’t seem to find a follow command, so I was wandering if anyone has any suggestions?
I’m looking for one object to follow another along one axis, so that as the first object moves, the second one moves parallel to it.
I have noticed the function LookAt, but can’t seem to modify it in this way, as whenever the first object hits an obstacle the paralleled follower simply continues, rather than keeping centred with the position of the first object.
Any help or advice is extremely appreciated as it’s been bugging me for weeks and I can’t seem to find a suitable solution.
The simplest solution would probably be to make the second object a child of the first, offset appropriately.
If that doesn’t answer your question, perhaps you could provide some more info about the problem. (I’m not sure from your description what it means to follow an object ‘along one axis’.)
Checked the API for ‘parent’ and it came back with some great results.
Just though I’d post it in case anyone else has the same question. I’ve also added a note below ‘localposition’ for expanded use.
Thanks again for the help
// Makes the camera follow this object by
// making it a child of this transform.
// Get the transform of the camera
var cameraTransform = Camera.main.transform;
// make it a child of the current object
cameraTransform.parent = transform;
// place it beind the current object
cameraTransform.localPosition = -Vector3.forward * 5;
// The above line can be expanded with additional Vector3 data. Also, by setting the ‘5’ as a variable it an easy access offset distance parameter.
// make it point towards the object
cameraTransform.LookAt(transform);