Hi all,
I am making top down 3d shooter game for android in C#, Unity version is 2017.1. I have waves of enemies coming. Up to 50 enemies maximum at a time, no more. What I want to achieve is to have a little marker for each enemy when it is not in camera viewport. What I have now is a simple red dot sprite that is only showing if enemy is not visible(I am using: OnBecameVisible() and OnBecameInvisible()). That works fine. What I can’t get right is sprites movement to be more precise. Below is what I have found on Google and adjusted it myself.
Vector3 enemyPosition;
public float speed;
void Update()
{
float cameraY = Camera.main.transform.position.y - 14;
if(cameraY <= 16.5f)
{
cameraY = 16.5f;
}
enemyPosition = new Vector3(transform.parent.position.x, cameraY,transform.parent.position.z);
float step = speed * Time.deltaTime;
transform.position = Vector3.MoveTowards(transform.position,enemyPosition, step);
Vector3 pos = Camera.main.WorldToViewportPoint(transform.position);
pos.x = Mathf.Clamp01(pos.x);
pos.y = Mathf.Clamp01(pos.y);
transform.position = Camera.main.ViewportToWorldPoint(pos);
}
At the moment sprite will mostly stay in the corners of the screen, sometimes jumping quickly from one corner to the adjacent one before settling. When I am almost in line with enemy than sprite will show precise location of the enemy on this axis. If I go lets say 1 unit down than sprite will go 2 units up from the line where enemy is standing, it seems to grow exponentially. This seems to be the cause of the jumpiness of sprite as well.
I am not to sure if Clamp01() is the best way to make those sprites behave better and point better to the parent enemy (red dot sprite is a child of the enemy).
Hope I have managed to explain it ok, if you need more info or pics shout.
Bear in mind that I am fairly new to Unity and horrible at math. I have spent last 2 days trying to get those dots behave. If you have better solution, proposition or links to where i can get myself educated on the subject please help. This is pretty much last coding wise problem I have in this otherwise cool project. CHEERS!
LoOnar Forge