How can i make my boomerang work in Unity2d?
Hello. I have been trying to make a basic boomerang that you can shoot by pointing and clicking, and the boomerang would turn at the spot you clicked and go back to the player. My problem occurs when my character is moving. When the character moves after throwing the boomerang, the boomerang will return to the location the character was when the boomerang hit the first targetPos instead of where the character is after the boomerang hits the first targetPos. I believe that this is because the second targetPos locks on to the character instead of constantly updating. but i couldnt figure out how i could fix this. How can i make the boomerang come directly to the character instead of going to the wrong position?
Script for boomerang:
[SerializeField] Transform target;
float speed = 6f;
Vector2 targetPos;
bool goback = true;
public Transform throwpoint;
private void Start()
{
targetPos = transform.position;
}
void Update()
{
if (Input.GetMouseButtonDown(0))
{
goback = false;
targetPos = (Vector2)Camera.main.ScreenToWorldPoint(Input.mousePosition);
target.position = targetPos;
}
if ((Vector2)transform.position != targetPos)
{
transform.position = Vector2.MoveTowards(transform.position, targetPos, speed * Time.deltaTime);
}
if ((Vector2)transform.position == targetPos)
{
targetPos = throwpoint.position;
transform.position = Vector2.MoveTowards(transform.position, targetPos, speed * Time.deltaTime);
}
}
}
Your answer
Follow this Question
Related Questions
How to create a spinwheel with pre-determined results after spin. 1 Answer
Rotating around z-axis with OnScreen buttons 1 Answer
Allowing unity game to have unlimited runs 0 Answers
How do you subtract a number from the Z position each time an object is destroyed? 1 Answer
How to create an object that mirrors players movements? 0 Answers