0

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

Thrillasel
  • 33
  • 6

1 Answers1

0

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.

Rus
  • 56
  • 3
  • Yeah it is pretty hard to say because there;s no obvious error. It's had me stumped for a couple of days so I decided to post it here. I just put it in update to get rid of any potential error from using a keydown or a timer. Lastly, I can't see which variable "value" you are on about. – Thrillasel Dec 11 '22 at 19:41
  • Is there anything else you can see that would cause the script not to run? Because nothing obvious sticks out to me, it *should* work – Thrillasel Dec 11 '22 at 19:43
  • I feel your pain In your image no 3, where you are showing that you selected one instantiated capsule prefab, you can see in the Inspector that the value of "value" variable is > 1. I suppose that value should be 0 so the object starts to travel By the way, what else can cause the problem is that traveling is happening very fast since you are managing it in your Update method Try to create a coroutine and slow down traveling just for testing I hope I explained it well – Rus Dec 12 '22 at 07:39
  • Unfortunately, both of those solutions do nothing :/. Any other ideas? – Thrillasel Dec 12 '22 at 18:30
  • Not at the moment but you can find me on discord and continue discussion there if you agree. It would be easier imho. rus89#7355 – Rus Dec 12 '22 at 20:33