I want to try using the new input system with the “Glitch Garden proyect” I’ve already install the system but dont know how to make it so it snaps to some spot like it did with the old way.
I’ve already tried to write something and figure it out on my on but…
yeah… I don’t know what I’m doing… I’m even having trouble naming omg.
It might be convenient for the course just to go with the old system but I just want to be able to make use of what is already consider like a better option.
I’m gonna keep watching videos and post a solution if I find it
I’m almost there already, I have been a whole day with that hahaha, but at least now I’m able to spawn my captus when I click. My problem now is with OnMouseDown since it doesn’t work with the new system. I’ll probably keep trying for another day before moving on with another lesson.
First of all, good job on challenging yourself. Did you change anything in the game? If not, the game area should have a Collider2D component attached. Before you implement any fancy logic, log a message into your console when you click on that collider. Once you accomplished that, you can use Rick’s algorithm for calculating the new defender’s position.
using UnityEngine;
using UnityEngine.InputSystem;
public class Controls : MonoBehaviour, CaptusSpawner.IPlayerInputActions
{
[SerializeField] GameObject captus;
CaptusSpawner inputActions;
private Vector2 SnapToGrid(Vector2 rawWorldPos)
{
float newX = Mathf.RoundToInt(rawWorldPos.x);
float newY = Mathf.RoundToInt(rawWorldPos.y);
return new Vector2(newX, newY);
}
private void Awake()
{
inputActions = new CaptusSpawner();
inputActions.PlayerInput.SetCallbacks(this);
}
public void OnClicked(InputAction.CallbackContext context)
{
if (context.performed)
{
// Debug.Log(context.action);
MyMouseClick();
}
}
private void MyMouseClick()
{
Vector2 mousePos = Camera.main.ScreenToWorldPoint(Mouse.current.position.ReadValue());
Vector2 gridPos = SnapToGrid(mousePos);
RaycastHit2D hit = Physics2D.Raycast(gridPos, Vector2.zero);
if (hit.collider)
{
// Call methods here
Instantiate(captus, gridPos, transform.rotation);
Debug.Log("Raycast Hit -> " + hit.transform.name);
}
}
private void OnEnable()
{
inputActions.PlayerInput.Enable();
}
private void OnDisable()
{
inputActions.PlayerInput.Disable();
}
}
with this code you have to tell to the lizards to ignore raycast in the layers if not since they have a collider too you can instantiate captus on top of them, and we will have to do that too for other object’s colliders, it seems there’s a way around that with:
layerMask https://docs.unity3d.com/ScriptReference/LayerMask.html but I have not try it yet.
I feel I’m getting more familiar with this system, I’ll keep going a bit more
At last, I had to try with a similar code, I think the previous one worked just fine but it didn’t snap cuz I made a mistake somewhere I’ll check and edit if needed.