Move forward a bit (in the direction of transform.right in 2D, or in 3D, transform.forward).
When you get close enough to the target, stop.
The problem with this procedure is that your sprite is moving like a vehicle, with a certain turn radius. It can’t turn any tighter than that. So, if the target is too close to the vehicle, it may circle it forever and never reach it.
But give it a try and experiment. There are certainly other ways to do it, including calculating how tight your turn radius should be based on the start position/angle and the end point, but this simple procedure is a good starting point.
But it don’t aproach to target. It can only move in your area and shoot at a distance to target (for now I will work in the movement, later in the shots).
If I use the script for movement in tutorials, I can move following the mouse, not by a mouse click. And, my sprite move without rotate
I have two boards to 10x10 squares each (for exmaple, the board can be bigger). In one of these is my player, in other the enemies.
This is the game mechanic:
I can move in my board, the enemies only in your board.
I can attack to the enemies from my board (shooting projectiles) and the enemies attack me from your board.
I can’t pass to enemie’s board and the enemies can’t pass to my board.
It is the mechanic. In this moment only I try to move correctly in my board my main player. The movement must be as the image upload in this post.
You can see it here:
White side is a player’s board, gray side is a enemie’s board.
OK, great. So start be removing any Rigidbody components from your game objects. Also stop using FixedUpdate; put your per-frame code in Update instead.
Now, try to implement the algorithm in my first reply above. But I guess we need to add some additional steps:
Detect a click on the game board. I would probably do this by just checking Input.GetMouseButtonDown in the Update event, and then casting a ray from the camera to see where on the board it hit. Print out this position hit with Debug.Log. Make sure you’ve got this before moving on to the next step.
Store the position clicked in a property called “target”.
Figure out the angle between the sprite’s current position and the target position, using Mathf.Atan2. Print this out with Debug.Log too. Check it manually to make sure it’s right.
Now, make your sprite turn toward the position clicked using Mathf.MoveTowardsAngle.
Next, make your sprite move forward at a steady rate.
Finally, add code to stop moving when the distance to the target is less than some minimum value.
Take these step by step. If you get stuck, ask for help, or go do some Unity tutorials — if you don’t know how to declare a property on a script, for example, then you really need to back up and work through some tutorials before attempting to make your own game.
But hang in there, and you will absolutely be able to do this sooner or later!