- Home /
pass a transform to a prefab
I am making a rocket launcher in which I select an object (its Transform component) and would like to pass the Transform to the rockets I launch (these are prefabs) in order to guide them to the position of the object, how could I pass the Transform component of the selection to these prefabs?
Answer by JxWolfe · Apr 09, 2020 at 06:53 PM
Vector 3s. Pass Vector3 pos, Vector3 rot (Euler angles), and Vector3 size for a full transform component. However, it looks like your example you would only need pos. However depending on your example, why bother passing it at all? If you have the game object selected have this:
void UpdateTarget () {
for(int i=0;i<rockets.Lenght;i++) {
rockets[i].targetpos = selected.transform.position;
}
}
Would that work? selected is the gameObject that you have clicked on, rockets is an array of the instantiated rocket prefabs "or GameObject.FindObjectsByType();" targetpos is a Vecotor3 that your targets point at as they move forward. Assuming you don't want them to instantly jerk over facing them, just lerp a point towards. Plenty of examples of that. Then the rockets would slowly adjust their projectory as they fly forward.
Your answer
Follow this Question
Related Questions
Prefab component not linking to destination 1 Answer
Reference Components with Iron Python 0 Answers
Add a component of type script dynamically at runtime. 0 Answers
Change script from EditorWindow 0 Answers
Export game objects with mesh paths 0 Answers