Hey guys, im following a tutorial to make a tower defense game.
But i stuck in 2 little problems.
One is they teach make how to pan the cam on map screen, its ok with the screen small than the map, but when you try to a screen bigger than it, it start shivering to every side.
Second, i come to the place where him start to place the tower ins the map, its ok, i can place the tower withou problem, but when i click in a place the tower is placed in the square above the one i clicked.
Someone can give me a little help how to solve this problems ?
Obs: im using my own art and the game is a 2d game.
Thanks
ok here is the camera movement(pan)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CameraMovement : MonoBehaviour
{
[SerializeField]
private float cameraSpeed = 0;
private float xMax;
private float ymin;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
private void Update()
{
GetInput();
}
//Controlador da camera do jogo pelo teclado
private void GetInput()
{
if (Input.GetKey(KeyCode.W))
{
transform.Translate(Vector3.up * cameraSpeed * Time.deltaTime);
}
if (Input.GetKey(KeyCode.A))
{
transform.Translate(Vector3.left * cameraSpeed * Time.deltaTime);
}
if (Input.GetKey(KeyCode.S))
{
transform.Translate(Vector3.down * cameraSpeed * Time.deltaTime);
}
if (Input.GetKey(KeyCode.D))
{
transform.Translate(Vector3.right * cameraSpeed * Time.deltaTime);
}
//Limitador da camera para não passar das bordas
transform.position = new Vector3(Mathf.Clamp(transform.position.x, 0, xMax), Mathf.Clamp(transform.position.y,ymin,0),-10);
}
//Limita a camera para não sair fora do mapa
public void SetLimits(Vector3 maxTile)
{
Vector3 wp = Camera.main.ViewportToWorldPoint(new Vector3(1, 0));
xMax = maxTile.x - wp.x;
ymin = maxTile.y - wp.y;
}
}
And here the place tower, it stay in the begin of place tower, he go implement other things, but for his tower the place worked ok (but mine dont, remember its my draws, but probably this dont affect the placement).
private void OnMouseOver()
{
if (Input.GetMouseButtonDown(0))
{
PlaceTower();
}
}
//Usado para adicionar Tower no cenário.
private void PlaceTower()
{
Instantiate(GameManager.Instance.TowerPrefab, transform.position, Quaternion.identity);
}
---------------------------------
[SerializeField]
private GameObject towerPrefab;
public GameObject TowerPrefab
{
get
{
return towerPrefab;
}
}
Hey guys, solved the problem in the tower position, not code problem, but in tiles pivot.
But i stuck in the camera shivering yet.
Someone can help with camera ?