How to follow cloned object with my prefab model?
Hello guys, I am trying to set the instantiated some patrolling clones as the target of the model (I call it Gita).
In my scenario, there will be many Gitas in the scene and each of them will follow the instantiated prefab once it shows up. (one Gita follows one target.)
I now have two C scripts, one for instantiate and the other for follow. Have searched lots relative articles on the community, however it still fails.
I am trying to let one gita follows the first cloned prefab, if it works then I can continue further. The only way to let the gita follow is that I manually assign the cloned prefab. Thanks beforehand!
1.Instantiate:
using UnityEngine;
using System.Collections;
public class Instantiate : MonoBehaviour
{
public GameObject newCustomer;
public GameObject customerPrefab;
public Transform barrelEnd;
void Update()
{
if (Input.GetKeyDown("z"))
{
newCustomer = Instantiate(customerPrefab, barrelEnd.position, barrelEnd.rotation) as GameObject;
}
}
}
2.Follow:
using UnityEngine;
using System.Collections;
public class FollowSmooth : MonoBehaviour
{
public Transform mTarget;
float mSpeed = 1f;
const float EPSILON = 0.1f;
// Use this for initialization
void Start ()
{
mTarget = GetComponent<Instantiate>().newCustomer.transform;
}
// Update is called once per frame
void Update()
{
transform.LookAt(mTarget.position);
if((transform.position - mTarget.position).magnitude > EPSILON)
transform.position = Vector3.MoveTowards(transform.position, mTarget.position, mSpeed * Time.deltaTime);
}
}
Your answer
![](https://koobas.hobune.stream/wayback/20220612214304im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
instantiate within function without clones 0 Answers
How to create 3 clones with Instantiate at the same time 0 Answers
[2D] Instantiate() duplicates former instances 1 Answer
Instantiate problem, spawning too many clones, then cloning the clones! help! 0 Answers
Object being flung off course as soon as it is touching clone of floor 0 Answers