- Home /
Question by
Ilez12 · Oct 27, 2021 at 02:09 PM ·
unity 2dprogramming
How to move two game objects on a limited area of the screen?
I'm creating a very simple 2D shooter. I have two game objects in my scene: player and crosshairs. The player has a gun that tracks crosshairs and the crosshairs follow the mouse (or finger on a touchscreen). The problem is that player stands still and he needs to move up and down to avoid obstacles. I tried using buttons - they work but take up too much screen space on touchscreen. I want a small area of the screen to be used for player control and the rest for crosshairs. The picture below must give a better understating...
Code for crosshairs:
{
public GameObject crosshairs;
private Vector3 target;
void Update()
{
target = transform.GetComponent<Camera>().ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, transform.position.z));
crosshairs.transform.position = new Vector2(target.x, target.y);
}
}
game.png
(342.8 kB)
Comment
Your answer
![](https://koobas.hobune.stream/wayback/20220613044655im_/https://answers.unity.com/themes/thub/images/avi.jpg)