Spawned Item Clone doesn't move like the original one

Viewed 48

I'm making a 2D game and i have spawning potion items in a making potion scene first of all i want to make the item pop up after spown and go like in this video that i recorded from my game:

Spawned Item do not move like the original

what do i do to make the spawned (Cloned) Item move the same as the original potion item?

Secondly, i want to spawn random and more than one item (potion) each time the scene starts how do i do that knowing that i'm using this script that spawns only one object:

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

public class HerbSpawner : MonoBehaviour
{
    //Here, we declare variables.
    public GameObject objToSpawn;
    public Transform groupTransform;


    //public means the var is exposed in the inspector, which is super helpful.


    // Start is called before the first frame update
    Vector2 spawnPos;

    void Start()
    {
        spawnPos = Camera.main.ViewportToWorldPoint(new Vector2(0.5f, 0.5f));
    }

    // Update is called once per frame
    void Update()
    {


        //let's also spawn on button press:
        if (Input.GetMouseButtonDown(0))
        {
            RaycastHit2D hit = Physics2D.GetRayIntersection(Camera.main.ScreenPointToRay(Input.mousePosition));

            if (hit.collider && hit.collider.CompareTag("Bush"))
            {
                SpawnIt();
            }
        }

        void SpawnIt()
        {
            Vector2 spawnPos = Camera.main.ViewportToWorldPoint(new Vector2(0.5f, 0.7f));
            Instantiate(objToSpawn, spawnPos, Quaternion.identity, groupTransform);
        }
    }
    }

please let me know if there is anyway to do it spawn multiple objects randomly and make the movement for the items to popup like in the video. This is teh script i used for that:

using System.Collections;
using UnityEngine;
using TMPro;

public class DragNDropItem : MonoBehaviour
{
    public string itemName;
    private bool dragging;
    private Vector2 firstPos;
    private Color spriteRenderer;
    [SerializeField] private float speed = 3;
    [SerializeField] private GameObject boiler;
    public TMP_Text itemNameText;
    private BoilerScript boilerScript;
    public AudioSource drop;

    void Awake()
    {
        // Initial position of the item
        firstPos = transform.position;
        boiler = GameObject.FindGameObjectWithTag("Boiler");
        boilerScript = boiler.GetComponent<BoilerScript>();
        spriteRenderer = gameObject.GetComponent<SpriteRenderer>().color;

    }
    void OnMouseDown()
    {
        dragging = true;
    }
    void OnMouseUp()
    {
        dragging = false;
        if (Vector2.Distance(transform.position, boiler.transform.position) < 0.7f) // We control the distance between the item and the cauldron without using the collider.
        {
            itemNameText.text = itemName;
            boilerScript.Potion(); // Checks the recipe's completion status each time an item is placed.
            spriteRenderer.a = 0f;
            gameObject.GetComponent<SpriteRenderer>().color = spriteRenderer;
            StartCoroutine(Alpha());
        }
        else drop.Play(); // If the item is left
    }

    void Update()
    {
        if (dragging) // As soon as the item is clicked with the mouse, the item follows the mouse.
        {
            Vector2 mousePosition = MousePos();
            transform.position = Vector2.Lerp(transform.position, mousePosition, speed * Time.deltaTime);
        } else if (!dragging && (Vector2)transform.position != firstPos) // As soon as we stop clicking, it takes it back to its original place. While the item is in its original location, we are constantly preventing the code from running.
        {
            transform.position = Vector2.Lerp(transform.position, firstPos, speed * Time.deltaTime);
        }
    }

    Vector2 MousePos() // position of mouse
    {
        return Camera.main.ScreenToWorldPoint(Input.mousePosition);
    }

    private IEnumerator Alpha() // After a second the item becomes visible.
    {
        spriteRenderer.a = 1f;
        yield return new WaitForSeconds(0.6f);
        gameObject.GetComponent<SpriteRenderer>().color = spriteRenderer;
    }

}
1 Answers

Your colliders aren't set to trigger. My guess is, when a clone is spawned, it collides with the existing potion object.

Related