hello, so in my project I have a camera that move with WASD and when the mouse is on the edge of the screen and it worked very well. But my project is kind of a city builder so to be able to place building I make an object follow the cursor, sice then the camera doesn’t move when the mouse is on the edge of the screenno matter what.
I think it might be because of my input system but I’m not sure.
camera movement script :
public class MoveCam : MonoBehaviour
{
[SerializeField] private float speed;
[SerializeField] private int screenEdge;
private Vector2 _moveInput;
private Rigidbody2D _rb;
// Start is called once before the first execution of Update after the MonoBehaviour is created
void Start()
{
_rb = GetComponent<Rigidbody2D>();
}
// Update is called once per frame
void Update()
{
_rb.linearVelocity = _moveInput.normalized * speed;
}
public void Move(InputAction.CallbackContext ctx)
{
_moveInput = ctx.ReadValue<Vector2>();
}
public void EdgeMove(InputAction.CallbackContext ctx)
{
if (ctx.ReadValue<Vector2>().y < screenEdge)
{
_moveInput.y = -1f;
print("work");
}
else if (ctx.ReadValue<Vector2>().y > Screen.height - screenEdge)
{
_moveInput.y = +1f;
print("work");
}
if (ctx.ReadValue<Vector2>().x < screenEdge)
{
_moveInput.x = -1f;
print("work");
}
else if (ctx.ReadValue<Vector2>().x > Screen.width - screenEdge)
{
_moveInput.x = +1f;
print("work");
}
if (ctx.ReadValue<Vector2>().y > screenEdge && ctx.ReadValue<Vector2>().y < Screen.height - screenEdge &&
ctx.ReadValue<Vector2>().x > screenEdge && ctx.ReadValue<Vector2>().x < Screen.width - screenEdge)
{
_moveInput.x = 0f;
_moveInput.y = 0f;
}
}
}
placement script :
public class Placement : MonoBehaviour
{
[SerializeField] private Batiment batiment;
[SerializeField] private bool plassable;
private Camera mainCam;
private Collider2D collider;
[SerializeField] private List<GameObject> bloking = new List<GameObject>();
public Material placementMat;
private Transform placementPosition;
private InputAction mousePos;
private InputAction button;
public TypeMana costMana1;
public TypeMana costMana2;
public TypeMana costMana3;
public int manaAmount1 = 0;
public int manaAmount2 = 0;
public int manaAmount3 = 0;
[SerializeField] private playerStat player;
private void Awake()
{
player = GameObject.Find("player").GetComponent<playerStat>();
}
// Start is called once before the first execution of Update after the MonoBehaviour is created
void Start()
{
mainCam = Camera.main;
collider = GetComponent<Collider2D>();
placementPosition = GetComponent<Transform>();
placementMat = GetComponent<Renderer>().material;
mousePos = InputSystem.actions["mousePosBatiment"];
button = InputSystem.actions["leftClickPlaceBat"];
}
// Update is called once per frame
void Update()
{
FollowMousePosition();
if (bloking.Count != 0 || EnoughMana())
{
print(bloking.Count);
print(EnoughMana());
plassable = false;
placementMat.SetColor("_Color", Color.red);
}
else
{
print("plassable");
plassable = true;
placementMat.SetColor("_Color", Color.green);
}
if (button.triggered)
{
Place();
}
}
private bool EnoughMana()
{
bool mana1 = false;
bool mana2 = false;
bool mana3 = false;
if (costMana1 != TypeMana.None)
{
if (player.getMana(costMana1) - manaAmount1 < 0)
{
mana1 = true;
}
else
{
mana1 = false;
}
}
else
{
mana1 = false;
}
if (costMana2 != TypeMana.None)
{
if (player.getMana(costMana2) - manaAmount2 < 0)
{
mana2 = true;
}
else
{
mana2 = false;
}
}
else
{
mana2 = false;
}
if (costMana3 != TypeMana.None)
{
if (player.getMana(costMana3) - manaAmount3 < 0)
{
mana3 = true;
}
else
{
mana3 = false;
}
}
else
{
mana3 = false;
}
if (mana1 || mana2 || mana3)
{
return true;
}
return false;
}
private void OnTriggerEnter2D(Collider2D collision)
{
if (collision.CompareTag("Batiment"))
{
bloking.Add(collision.gameObject);
}
}
private void OnTriggerExit2D(Collider2D collision)
{
if (collision.CompareTag("Batiment"))
{
bloking.Remove(collision.gameObject);
}
}
public void Place()
{
if (plassable)
{
print("performed");
Payment();
Instantiate(batiment, placementPosition.position, placementPosition.rotation);
Destroy(gameObject);
}
}
private void Payment()
{
if (costMana1 != TypeMana.None)
{
player.subMana(costMana1,manaAmount1);
}
if (costMana2 != TypeMana.None)
{
player.subMana(costMana2,manaAmount2);
}
if (costMana3 != TypeMana.None)
{
player.subMana(costMana3,manaAmount3);
}
}
private void FollowMousePosition()
{
placementPosition.position = GetWorldPosition();
}
private Vector2 GetWorldPosition()
{
return mainCam.ScreenToWorldPoint(mousePos.ReadValue<Vector2>());
}
}
my input system :
I’ve tryed to put them on the same input but that didn’t fix enything, I don’t really know what to try, so eny idea/tips ?
thank in advance for your wisdom.

