[Solved] Move a spawned object and get if collision occurs

Hello everyone,
I’m trying to solve a simple problem but with my code I’m having issues…
I have created an empty object in a 3D environment. Now I want that this object spawns spheres and make them move in a single direction with a linear speed. When there is a collision with a character they start a trigger. This is my code:

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

public class SpawnSpheres : MonoBehaviour {

    public float MoveSpeed = 10;

    // Use this for initialization
    void Start () {
       
    }

    // Update is called once per frame
    void Update()
    {
        if (Input.GetKeyDown("q"))
        {
            GameObject sphere = GameObject.CreatePrimitive(PrimitiveType.Sphere);
            sphere.transform.localScale = new Vector3(0.2f, 0.2f, 0.2f);
            //            sphere.AddComponent<Rigidbody>();
            sphere.transform.position = new Vector3(2.3f, 1.5f, 10);
            //First method*****************************
            sphere.transform.position = Vector3.MoveTowards(transform.position, new Vector3(2.3f, 1.5f, 0), MoveSpeed * Time.deltaTime);
        }
        if (Input.GetKeyDown("w"))
        {
            GameObject sphere = GameObject.CreatePrimitive(PrimitiveType.Sphere);
            sphere.transform.localScale = new Vector3 (0.2f,0.2f,0.2f);
            //            sphere.AddComponent<Rigidbody>();
            sphere.transform.position = new Vector3(1.7f, 1.5f, 10);
            //Second method*************************
            sphere.transform.Translate(Vector3.forward * MoveSpeed * Time.deltaTime);
        }
    }
    void OnCollisionEnter(Collision col)
    {
        if (col.gameObject.name == "male_01")
        {
            //            Destroy(col.gameObject);
            System.Console.WriteLine("collision");
        }
    }
}

Each method is not working. Spheres are not moving. So I can’t check if interaction with my model is working.
Let me know, please, how to solve this.
Thanks

that script is on the empty gameobject?

Input.GetKeyDown valid for only a single frame, so all the code in those scopes will only execute once.
OnCollisionEnter checks for collisions with the current gameobject, ie the one the script is attached to, ie the empty without a collider that has nothing to do with the spheres

In short, you appear to have put everything into one script on one object… this should be broken up into scripts on the appropriate objects. I’d recommend working with a prefab with it’s own scripts over a primitive.

You might want to look through some of the tutorials in the learn section, specifically the spawning of projectiles in the space shooter since you want things to appear and move in a direction.

1 Like

I’m working to divide the behaviour of each “sphere” and the spawner object. I think that by this way I will solve this. Thanks
I will answer asap

Script on the spawner:

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



public class SpawnSpheres : MonoBehaviour {

    public GameObject MovingSphereL;
    public GameObject MovingSphereR;

    // Use this for initialization
    void Start () {
       
    }

    // Update is called once per frame
    void Update()
    {
        if (Input.GetKeyDown("q"))
        {
            Instantiate(MovingSphereL, new Vector3(1.7f, 1.5f, 10), Quaternion.identity);
            //            sphere.AddComponent<Rigidbody>();
        }
        if (Input.GetKeyDown("w"))
        {
            Instantiate(MovingSphereR, new Vector3(2.3f, 1.5f, 10), Quaternion.identity);
            //            sphere.AddComponent<Rigidbody>();
        }
        if (MovingSphereL.transform.position.z == 0)
        {
            Destroy(MovingSphereL);
        }
        if (MovingSphereR.transform.position.z == 0)
        {
            Destroy(MovingSphereR);
        }
    }

}

Script on the spheres:

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

public class MoveForward : MonoBehaviour {

    public float MoveSpeed = 1;

    // Use this for initialization
    void Start () {
       
    }
   
    // Update is called once per frame
    void Update () {
        transform.Translate(Vector3.back * MoveSpeed * Time.deltaTime);
    }

    void OnCollisionEnter(Collision col)
    {
        print(gameObject.name + "collided with another object");
        if (col.gameObject.name == "male_01")
        {
            print(gameObject.name + "collided with another object");
            Debug.Log ("Hit", gameObject);
        }
    }
}

It has been not so hard. Thanks for your suggestion. I hope this will help someone else.