1
\$\begingroup\$

I want to move objects on the screen horizontally. And the user has to pop the object by touching it. When user touch it, it gets destroyed and new object comes randomly.

This is the draft image showing moving objects from right to left.

For experimenting, I used Unity's Scroll Rect to move them. What can be the best solution for this.

\$\endgroup\$
  • \$\begingroup\$ Are you developing 2D or 3D? And is your camera set to orthographic? \$\endgroup\$ – Hamza Hasan Jan 25 '16 at 10:00
  • \$\begingroup\$ im making 2D game. yes my camera is set to orthographic. \$\endgroup\$ – Saad Anees Jan 27 '16 at 10:38
1
\$\begingroup\$

Do not use scroll rect , if the objects are not ui elements. The simple solution will be to make one parent object and make all these elements as child. Then use offset to set local position for all the child elements. To move them you just need to move the parent object. And to add new elements add them to the parent with local position defined by the offset.

\$\endgroup\$
1
\$\begingroup\$

The best solution near me to do so is to attach a small script to every object, that will move your object and handle touch to itself.

You can create a script that'd attach to your objects like,

Assumptions:

I am moving object towards -ve x-axis. You can play around with this.

using UnityEngine;
using System.Collections;

public float _speed = 0.01f;

public class MoveObject : MonoBehaviour
{

    // Use this for initialization
    void Start ()
    {

    }

    // Update is called once per frame
    void Update ()
    {
        transform.position = Vector3.MoveTowards (transform.position, new Vector3 (transform.position.x - 0.1f, transform.position.y, transform.position.z), _speed);
    }

    void OnMouseDown ()
    {
        Destroy (gameObject);
    }

    void OnDestroy ()
    {
        // TODO: Create another object here
        Debug.Log (name + " destroyed");
    }
}
\$\endgroup\$
  • \$\begingroup\$ i guess in this way im only moving them but not adding new elements \$\endgroup\$ – Saad Anees Jan 27 '16 at 10:41
  • \$\begingroup\$ in OnDestroy you can add new element \$\endgroup\$ – Hamza Hasan Jan 27 '16 at 10:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.