2
\$\begingroup\$

I have two character in my game: enemy and main character. Enemy can throw different objects into the main character. For this moment i am doing this action in such way:

void FixedUpdate(){
    if (CanMove){
        transform.position = Vector2.MoveTowards(transform.position, TargetPlayer.transform.position,  15 * Time.deltaTime);
    }
}

public void StartMove(bool canMove){
    CanMove = canMove;
    TargetPlayer = GameObject.FindGameObjectWithTag(Tag).transform;
    rigidbody2D.gravityScale = 0;
}

This script is attached to the prefab of the object that will be thrown into main character by the enemy. This script is working, but in this case the object that was thrown will follow by main character all the time, because of this line of code: transform.position = Vector2.MoveTowards(transform.position, TargetPlayer.transform.position, 15 * Time.deltaTime);

To my mind this is not good, because i want to give my player the opportunity to escape from the object that was thrown at him. For example run away. When the object reaches the player position (and the player is on the new position) - he will simply fall onto the ground. I did this, but this wasn't looking nice, because when the object was reaching his position, he was hanging in the air. Can anyone give me an advice how to make this process more good-looking?

Edit: I need to make good-looking movement of the object, that was thrown by the enemy into main character. Every frame i am moving the instantiated object to the current player position. So the player can't escape from that object, because he always knows current player position. I think that this is not good idea, and i want to give to player an opportunity to run away from that object. In this case i am saving the position of the player into variable, and then every frame i am moving the instantiated object to that position. So when the player run away, the object, that was thrown doesn't know anything about new position of my player. But this is looking not good, because when the object is reaches its destination position he hangs in the air((

Edit # 2: I was thinking about destroying that object anyway, but to my mind this will not look good: the object is moving and suddenly disappears from screen(. May there is some ways how to make him smoothly fall onto the ground? The problem is in that this instantiated object has no Rigidbody2d component attached to it, so it will never fall down by itself.

\$\endgroup\$
7
  • \$\begingroup\$ Can you try asking a more specific question. I am not sure if you are asking a graphical or logistical question about which object. Are you referring to your character falling over? Are you asking about how to make your object stop homing on the characters position? \$\endgroup\$
    – Shroeder
    Oct 29, 2014 at 15:45
  • \$\begingroup\$ Shroeder, i have edited my post. \$\endgroup\$
    – bukka.wh
    Oct 29, 2014 at 15:57
  • \$\begingroup\$ Have you tried saving the characters location at the time of the object being thrown? In that way you can have the missile go to that location vs an updated one. When it comes to making the animation "look good", are you referring to its appearance once it has come into contact with the character? \$\endgroup\$
    – Shroeder
    Oct 29, 2014 at 16:00
  • \$\begingroup\$ Yes, I am saving the characters location. But in this case when the object reaches its destination point (and the player is far away from that point) it simply hangs in the air and this is not looking good. But when the player still is on that point - then everything is ok: i detect collision and destroy this object. \$\endgroup\$
    – bukka.wh
    Oct 29, 2014 at 16:17
  • \$\begingroup\$ Why not destroy the object no matter what then? Either it hits or misses, but in both cases you want it to be "destroyed" yes? Or are you suggesting you want it to follow the player in the case that he is no longer at the destination point? \$\endgroup\$
    – Shroeder
    Oct 29, 2014 at 16:19

1 Answer 1

3
\$\begingroup\$

Instead, get the direction from the origin to the target. Instead of moving the projective towards a destination, move it in a direction. Something like:

// Determine the direction
Vector2 direction = (origin - direction).normalized;

Then something like:

void FixedUpdate()
{
    if (CanMove)
    {
        transform.position = transform.position + (direction * speed * Time.deltaTime);
    }
}

Move it until it hits something (goes off camera, or hits the ground or a building, platform, tree, whatever) and THEN destroy it.

You can also transition the end collision in some way rather than a simple Destroy(gameObject) by playing a sound effect, swapping the sprite, etc. in order to avoid the jarring disappearance of the object.

\$\endgroup\$
0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .