- Home /
GameObject follow Tagged Object
Hi,
So I currently have an object that stays in front of a tagged object based on the following code:
target = GameObject.FindWithTag("Player").transform;
transform.parent = target.transform;
transform.localPosition = Vector3(0, 1, 2);
The problem is that I don't want the object to become a child of the tagged object. I'm not sure how to do this, I've tested a few methods, but to no avail.
just out of curiosity, why don't you want it to be parented?
I want the object to be clickable and it seems that when I set up the object with the code from above it no longer reacts to the On$$anonymous$$ouseDown function
Answer by Anxo · Jun 14, 2011 at 10:13 PM
var objectToFollow : Transform;
transform.position.x = objectToFollow.position.x;
transform.position.y = objectToFollow.position.y+1;
transform.position.z = objectToFollow.position.z+2;
but this will be a very harsh follow, you could smoothen that out with mathf.lerp
the problem with this is that the objectToFollow isn't in the hierarchy until play is pushed because the user gets to choose their avatar. So I can't put the objectToFollow in the necessary place in the inspector. I can't search for the tag of the object using "GameObject.FindWithTag("Player")" because I get an error saying:
"'position' is not a member of the 'UnityEngine.GameObject'."
you just have to ad transform after it. when you use find with tag your looking for a game object. to access the transform thereof just use
objectToFollow = GameObject.FindWithTag("Player").transform;
sweet it works.
How do I make it stay in front of my character though cause now I turn and it doesn't stay in front of me.