Adding force to instantiated objects

Hi everyone,

I seem to have everything right from what I can see across other people’s posts but I’m still learning so may be being silly… :slight_smile:

I’m trying to spawn in obstacles for an endless scroller and add force to push them towards the player. Currently they spawn and don’t move at all.

Anyone have any ideas please?

Obstacle Mover script…

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class ObstacleMover : MonoBehaviour
{
    [SerializeField] GameObject[] obstacles;
    GameObject instance;

    [SerializeField] Vector2 conveyorForce;
    ObstacleSpawner obstacleSpawner;


    void FixedUpdate()
    {
        // Calling the clones from the ObstacleSpawner CS
        instance = obstacleSpawner.instance;

        //Adding force to the instantiated objects
        instance.GetComponent<Rigidbody2D>().AddForce(conveyorForce * 1000);
    }
}

Obstacle Spawner script…

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class ObstacleSpawner : MonoBehaviour
{
    Vector3 spawnPos;

    // Timer for spawning
    [SerializeField] float spawnTimer;

    // The parent obstacles to clone
    [SerializeField] GameObject [] obstacles;

    // The clones
    public GameObject instance;

    void Start()
    {
       
    }

    void FixedUpdate()
    {
        spawnTimer = spawnTimer - Time.fixedDeltaTime;
    }

    void Update()
    {
        if (spawnTimer <= 0f)
        {
            var instance = Instantiate(obstacles[Random.Range(0, 4)], transform.position, transform.rotation);
            Debug.Log("Obstacle Spawned");
            spawnTimer = 5f;
        } 
    }


}

I am assuming you confirmed that the conveyorForce > 0 in some axis? Newly instantiated objects would have the default value (0,0) for Vector2.

Sorry yes it is indeed, it was set to -40 which works if I test it on the parents before instantiating. I’ve even tried -10000 just to see if I wasn’t adding force. :frowning:

I think it’s the referencing between the two scripts but I can’t seem to get it to work.

Where are you setting the conveyorForce, though? As I added above, each new instantiated object starts off with the defaults, so nothing you modified in the Inspector on a test object will be applicable. Code must modify the conveyorForce after instantiation.

1 Like

Thanks so much. I worked on this and then noticed the real error.

The issue was that I was only adding force when the spawnTimer hit <= 0, so I also added a constant force for spawnedInstance GameObject when the spawnTimer reset back to the 1.5f (now) mark.

Again, thank you.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class ObstacleSpawner : MonoBehaviour
{
    float spawnTimer = 1.5f;    // Timer for spawning
    public Vector2 conveyorForce;   // The force to apply
    [SerializeField] GameObject [] obstacles;  // The parent obstacles to clone
    public GameObject spawnedInstance;     // The clones

    void FixedUpdate()
    {
        spawnTimer = spawnTimer - Time.fixedDeltaTime;

        if (spawnTimer <= 0f)
        {
            spawnedInstance = Instantiate(obstacles[Random.Range(0, 4)], transform.position, transform.rotation);
            spawnTimer = 1.5f;
            conveyorForce = new Vector2(-300, 0 * Time.fixedDeltaTime);
            spawnedInstance.GetComponent<Rigidbody2D>().AddForce(conveyorForce);
        }

        if (spawnTimer > 0.1f)
        {
            spawnedInstance.GetComponent<Rigidbody2D>().AddForce(conveyorForce * Time.fixedDeltaTime);
        }
    }

}
1 Like