using System.Collections;
using System.Collections.Generic;
using System.Numerics;
using UnityEngine;
using UnityEngine.UI;
public class JumpSpawn : MonoBehaviour
{
public GameObject Jump;
public Button jumpButton;
private bool canSpawn = false;
public LayerMask Ground;
public float spawnRadius = 0.5f;
public float snapFactor = 1f;
void Start()
{
jumpButton.onClick.AddListener(OnJumpButtonClick);
}
public void OnJumpButtonClick()
{
canSpawn = true;
}
void Update()
{
if (canSpawn && Input.GetMouseButtonDown(0))
{
UnityEngine.Vector2 mousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
//RaycastHit2D hit = Physics2D.Raycast(mousePos, UnityEngine.Vector2.zero, Mathf.Infinity, Ground);
GameObject j = Instantiate(Jump, mousePos, UnityEngine.Quaternion.identity);
//Instantiate(Jump, mousePos, UnityEngine.Quaternion.identity);
canSpawn = false;
}
}
UnityEngine.Vector2 SnapPosition2D(UnityEngine.Vector2 input, float factor = 1f)
{
if (factor <= 0f)
throw new UnityException("factor argument must be above 0");
float x = Mathf.Round(input.x / factor) * factor;
float y = Mathf.Round(input.y / factor) * factor;
return new UnityEngine.Vector2(x, y);
}
}
So whenever I spawn the jump button, I wanted it to automatically snap to grid, I can’t seem to make it work in my code