c#/2D - How to move object to another moving object that's been clicked on?

Hey Guys,

First off I want to say that this forum has been incredibly resourceful for a beginner game developer such as myself. I want to say a big thank you to everyone contributing to this community :).

Now for my problem.

I have a game where you play a frog that can move along the x xis and he needs to eat flies. The main mechanic is the the frog’s tongue. I have it set up so that on mouse down (anywhere on the screen) a game object spawns at the frog position and moves towards the Camera.main.ScreenToWorldPoint(Input.mousePosition); with a linerenderer to create the tongue itself. Upon reaching the target position, the game object will move back to the frog position.

Flies spawn on the side of the screen and move along the x axis in a zig zag pattern which is all fine.

After play testing with a few people I found that when you tap a fly the tongue will shoot out and move to the position you tapped. It takes a short time for the tongue to reach that point and by the time it does, the fly is no longer in that position. So the game required you to predict where the fly would go.

So a lot of players found this frustrating and I now believe for the game to be more fun i need to change it so when players tap the fly the tongue should just move towards the fly’s position and not the position of the screen they tapped.

I dont know if I woke up on the wrong side of the bed this morning but when I tried to implement this change after around 3 hours my brain decided to explode and force me to ask for help.

I’ve attempted using ray casts to detect the object being clicked and then move the instatiated tongue object prefab to the hit object, but of course this will only return the position of the hit object when clicked.

Heres a snippet of the code in c#

void Update () {


	if(Input.GetMouseButtonDown(0)&&(!shooting)){
			Vector3 target = Camera.main.ScreenToWorldPoint(Input.mousePosition);
			RaycastHit2D hit = Physics2D.Raycast(Camera.main.ScreenToWorldPoint(Input.mousePosition), Vector2.zero);

			shooting = true;

			GameObject InstanceTongueHitBox = (GameObject) Instantiate(tongueHitBox, transform.position, Quaternion.identity);
			GameObject InstanceTargetBox = (GameObject) Instantiate(targetBox, target, Quaternion.identity);

			if(hit.collider != null)
			{
				Debug.Log(hit.collider.gameObject.name);
				Debug.Log(hit.collider.transform.position);
				LeanTween.moveLocal ( InstanceTongueHitBox,  hit.collider.transform.position,  0.25f);
			}else{
				LeanTween.moveLocal ( InstanceTongueHitBox,  target,  0.25f);
			}
 }

I hope there’s a simple solution to this and i feel like there is, but being a beginner I’ve really got no clue at this point. I would really appreciate some help on this one. Is there a better way to go about this all together?

Thank again guys!

  • Jay

2 Answers

2

I would go about it this way:

//Put this into your fly object

void OnMouseEnter()
{
     if (Input.GetMouseButtonDown(0) && frogScript.tongueAlreadyMoving == false)
     {
         frogScript.flyTransform= transform.position; //we get fly transform
         frogScript.flyGameObject= gameObject;   //we get fly gameobject
         frogScript.tongueAlreadyMoving = true;  //tell tongue to move here
     }

}

//And now for the tongue put this frogScript into frog

public static Transform flyTransform;
public static GameObject flyGameObject;
public static bool tongueAlreadyMoving = false;

void FixedUpdate()
{
      if (tongueAlreadyMoving == true)
      {
          //Though this is with rigidbody2D
          Vector2 flyDirection = (target.position - transform.position).normalized;
          rigidbody2D.AddForce(flyDirection * tongueSpeed); //tongue will go towards fly
      }
}

void OnTriggerEnter2D(Collider2D col)
{
    //Not sure this will work as expected
    if(col.Collider2D.gameObject = flyGameObject)
    {
         //Destroy the fly
         //and go with your tongue back to frog
   

    }
}

the basic idea i`m also not expert in C# but at least it helps a bit for u

Set a tag for the flies and check if the hitted object is a fly. If it is, cache its transform and if it moves, cancel the tween and set it to the updated position.

private Transform targetTransform;
private Vector3 lastTargetPosition;
private GameObject InstanceTongueHitBox;

void Update () {
    if (!LeanTween.isTweening)
    {
        targetTransform = null;
        InstanceTongueHitBox = null;
    }

    if (InstanceTongueHitBox != null && targetTransform != null && targetTrasform.position != lastTargetPosition)
    {
        LeanTween.cancel(InstanceTongueHitBox);
        LeanTween.moveLocal ( InstanceTongueHitBox,  targetTransform.position,  0.25f);
        lastTargetPosition = targetTransform.position;
    }
        
 
    if(Input.GetMouseButtonDown(0)&&(!shooting)){
            Vector3 target = Camera.main.ScreenToWorldPoint(Input.mousePosition);
            RaycastHit2D hit = Physics2D.Raycast(Camera.main.ScreenToWorldPoint(Input.mousePosition), Vector2.zero);
 
            shooting = true;
 
            InstanceTongueHitBox = (GameObject) Instantiate(tongueHitBox, transform.position, Quaternion.identity);
            GameObject InstanceTargetBox = (GameObject) Instantiate(targetBox, target, Quaternion.identity);
 
            if(hit.collider != null && hit.tag == "YourFliesTag")
            {
                Debug.Log(hit.collider.gameObject.name);
                Debug.Log(hit.collider.transform.position);
                LeanTween.moveLocal ( InstanceTongueHitBox,  hit.collider.transform.position,  0.25f);
                targetTransform = hit.collider.transform;
                lasttargetPosition = hit.collider.transform.position;
            }else{
                LeanTween.moveLocal ( InstanceTongueHitBox,  target,  0.25f);
                targetTransform = null;
                InstanceTongueHitBox = null;
            }
 }

I haven’t used LeanTween so I don’t know if canceling the tween makes the GameObject move to the original position. If it does, try using pause() instead.

What this does is storing the transform of the fly and the InstanceTongueHitBox. If it has moved - its position is different from its last position - it cancels the actual tween and sets a new one.