Search code examples
c#unity-game-enginemove

Unity C# Make two Sprites move to each other


So I'm new to Unity, but creating a simple 2D game. I need to make two sprites move to each other when clicking on them. I want to use a script attached to the Main Camera but open for other suggestions. Thanks friends! Here is my script:

public class MoveTo : MonoBehaviour {
	GameObject objectA = null;
	GameObject objectB = null;

	// Use this for initialization
	void Start () {
		
	}
	
	// Update is called once per frame
	void Update () {
		
		
		if(Input.GetMouseButtonDown(0))
		{
			Ray rayOrigin = Camera.main.ScreenPointToRay(Input.mousePosition);
			RaycastHit hitInfo;
			
			if(Physics.Raycast(rayOrigin, out hitInfo))
			{
				//Destroy (hitInfo.collider.gameObject);
				if(objectA == null)
				{
					objectA = hitInfo.collider.gameObject;

				}
				else{
					objectB = hitInfo.collider.gameObject;
				}
				if(objectA != null && objectB != null)
				{

					//Need something to make them move towards each other here???

					objectA = null;
					objectB = null;
		
				}
				
			}
		}
	}
}


Solution

  • In my opinion moving other gameObjects with a script attached to the camera is breaking the basic idea of components in Unity. It would also be very hard to do if you want the objects to move smoothly and also be able to have multiple pairs of objects moving at same time. You would need some kind of list of moving objects and their destinations. Then you would need to go through the list in the update function of the camera script and update all positions of sprite gameObjects.

    I think it is better to attach simple inbetweening script like below to the gameObjects of the sprites.

    using UnityEngine;
    using System.Collections;
    
    public class Tweener : MonoBehaviour {
    
        public float tweenStopDistance = 0.2f;
        public float tweenSpeed = 2.0f;
        public Vector3 targetPosition = new Vector3();
    
        void Start () {
            targetPosition = transform.position;
        }
    
        void Update () {
            if((transform.position - targetPosition).magnitude > tweenStopDistance){
    
            transform.position += tweenSpeed * (targetPosition - transform.position).normalized * Time.deltaTime;
            }
        }
    }
    

    In this case you just need to calculate the target position in your camera script.

    if(objectA != null && objectB != null)
        {
    
        // Calculate position in the middle
        Vector3 target = 0.5f * (objectA.transform.position + objectB.transform.position);
    
        // Set middle position as tween target
        objectA.GetComponent<Tweener>().targetPosition = target;
        objectB.GetComponent<Tweener>().targetPosition = target;
    
        objectA = null;
        objectB = null;
    
    }
    

    If you have lot of those sprites and not much computation power on your target machine. You could also attach that script component at run time with gameObject.AddComponent for those sprites that need it. Attaching is probably quite heavy but you wouldn't need to test for already being in the target for sprites that are not moving.