The question is answered, right answer was accepted
Touch Camera Movement and Transform Player on Touch
Hey All, be kind i am just learning Unity and C#. My Goal is to make a tile strategy game, in which you can scroll across a TileMap Board and then select a tile and move a player Entity to the selected tile.
i am having an issue with my touch interaction with my camera verse the selection of the tile to move the player. I am developing for Android right now so it will be a touch screen. Every time i try to just move my camera i place my player on the first tile i select when i just want to scroll ac cross the TileMap. I would like to figure out a clean way go across the map with out placing the player unless i tap a tile. I have played around with making a flag if TouchPhase.Moved =true dont place the player but it just places it anyway since the initial TouchPhase is not Moved but Began.
I feel like having competing updates in 2 scripts is a bad idea....
Camera Scroll Function
public GameObject camera_GameObject;
Vector2 StartPosition;
// Update is called once per frame
void Update()
{
if (Input.touchCount == 1)
{
if (Input.GetTouch(0).phase == TouchPhase.Moved)
{
Debug.Log(Input.GetTouch(0).phase.ToString());
Vector2 NewPosition = GetWorldPosition();
Vector2 PositionDifference = NewPosition - StartPosition;
camera_GameObject.transform.Translate(-PositionDifference);
}
StartPosition = GetWorldPosition();
}
}
Vector2 GetWorldPosition()
{
return camera_GameObject.GetComponent<Camera>().ScreenToWorldPoint(Input.mousePosition);
}
Player Tile Move Script ( This is Attached to my Player Object)
public Tilemap tilemap;
public void Update()
{
TargetPosition();
}
public void TargetPosition()
{
if (Input.GetMouseButtonDown(0))
//if (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Began) //<-- Used for Touch Screen only
{
Debug.Log("Mouse DOWN");
if (!EventSystem.current.IsPointerOverGameObject(Input.GetTouch(0).fingerId))
{
Vector3 mouseWorldPos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
Vector3Int target = tilemap.WorldToCell(mouseWorldPos);
transform.position = tilemap.GetCellCenterWorld(target); // Move there
SavePlayerPosition();
GameControl.control.platinum += 1;
}
else { Debug.Log("Detected GameObject On Touch"); }
}
}
Found a solution! $$anonymous$$y problem was my logic in my IF statements in the camera and the bool switch. I also had to move my action for the player into the camera script
public class Camera2 : $$anonymous$$onoBehaviour
{
public Tilemap tilemap;
public GameObject camera_GameObject;
Vector2 StartPosition;
private bool _moved;
public Player player;
// Update is called once per frame
void Update()
{
if (Input.touchCount == 1)
{
if (Input.GetTouch(0).phase == TouchPhase.$$anonymous$$oved)
{
Vector2 NewPosition = GetWorldPosition();
Vector2 PositionDifference = NewPosition - StartPosition;
camera_GameObject.transform.Translate(-PositionDifference);
_moved = true; // Remember that the player moved
}
StartPosition = GetWorldPosition();
}
if (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Ended)
{
if (!_moved)
{
TargetPosition();
}
_moved = false;
}
}
Vector2 GetWorldPosition()
{
return camera_GameObject.GetComponent<Camera>().ScreenToWorldPoint(Input.mousePosition);
}
public void TargetPosition()
{
Debug.Log("$$anonymous$$ouse DOWN");
if (!EventSystem.current.IsPointerOverGameObject(Input.GetTouch(0).fingerId))
{
Vector3 mouseWorldPos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
Vector3Int target = tilemap.WorldToCell(mouseWorldPos);
player.transform.position = tilemap.GetCellCenterWorld(target); // $$anonymous$$ove there
SavePlayerPosition();
GameControl.control.platinum += 1;
}
else { Debug.Log("Detected GameObject On Touch"); }
}
public void SavePlayerPosition()
{
GameControl.control.PlayerX = transform.position.x;
GameControl.control.PlayerY = transform.position.y;
GameControl.control.PlayerZ = transform.position.z;
}
}