Instantiated object doesn't follow iTween path, how do I fix this?

39 Views Asked by At

So essentially I've:

  • Instantiated a new prefab of an object

  • Set all the variables in the transform of the prefab

  • Copied the script for the initial capsule (which works) into the prefab spawning script

And yet my prefabs for some reason really do not want to follow the path around, how do I make them follow the path?

    public class PathFollower : MonoBehaviour

{
    public float movementSpeed;
    //private float pathProgress = 0;
    public GameObject objectToSpawn;
    public Transform[] positionPoint;
    [Range(0,1)]
    public float value;

    void Start()
    {
        Debug.Log(iTween.PathLength(positionPoint));
    }
    void Update()
    {
        movementSpeed = 10f;
        if (value < 1)
    {
            value += Time.deltaTime / movementSpeed;
    }
        iTween.PutOnPath(objectToSpawn, positionPoint, value);
    }
    private void OnDrawGizmos()
    {
        iTween.DrawPath(positionPoint,Color.green);
    }
}
public class deployCapsule : MonoBehaviour
{
    public float movementSpeed;
    public Transform[] positionPoint;
    public GameObject CapsulePrefab;
    [Range(0, 1)]
    public float value;
    //movementspeed = 10f;

    // Start is called before the first frame update
    void Start()
    {

    }


    void Update()
    {
        
        GameObject a = Instantiate(CapsulePrefab) as GameObject;
        a.transform.position = Camera.main.ScreenToWorldPoint(new Vector3(Screen.width - 20, Screen.height - 20, 10));
        //if (Input.GetKeyDown(KeyCode.Space))
        //{
        a.GetComponent<PathFollower>().positionPoint = positionPoint;

        //}
        movementSpeed = 10f;
        if (value < 1)
        {
            value += Time.deltaTime / movementSpeed;
        }
        iTween.PutOnPath(a, positionPoint, value);
        
    }
}

Where the scripts are attached, also shows transform arrays:

Main Camera - deployCapsule

Capsule - PathFollower

Capsule Prefab - PathFollower

1

There are 1 best solutions below

5
Rus On

Hard to say what is the exact problem, but from the image no 3 I see that value of variable value is 1 which may cause the problem.

It should be 0 so the object is at the beginning of the path.

You can use Start method to explicitly set the value of the value variable

Despite this, I wouldn't recommend you to instantiate game objects every frame (in Update method) because it's not efficient and probably will never have such use case.