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.