New Input system

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…

image
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

1 Like

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.

Hi Gin,

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.

Good luck! :slight_smile:

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.

using UnityEngine;


public class GetACaptus : MonoBehaviour
{

    [SerializeField] GameObject captus;
    CaptusSpawner inputs;


    private void Awake()
    {
        inputs = new CaptusSpawner();
    }
    private void OnEnable()
    {
        inputs.Enable();
    }
    private void OnDisable()
    {
        inputs.Disable();
    }

    private void Start()
    {
        inputs.PlayerInput.Clicked.started += _ => DetectObject();
    }
   
    private void DetectObject()
    {
        Vector2 mousePos = Camera.main.ScreenToWorldPoint(inputs.PlayerInput.Position.ReadValue<Vector2>());
        Vector2 gridPos = SnapToGrid(mousePos);
        RaycastHit2D hits2D = Physics2D.Raycast(gridPos, Vector2.zero);
        if (hits2D.collider != null)
        {
            
            Debug.Log("2D hit" + hits2D.collider.tag);
          Instantiate(captus, gridPos, transform.rotation);

        }
    }


    private Vector2 SnapToGrid(Vector2 rawWorldPos)
    {
        float newX = Mathf.RoundToInt(rawWorldPos.x);
        float newY = Mathf.RoundToInt(rawWorldPos.y);
        return new Vector2(newX, newY);
    }
}

I don’t know if is weird how this is workin without using UnityEngine.InputSystem;

I attached the scrip to the object with the collider where I wanted to click.
And to look for the mous posicion I had to add that here.

Good job on solving the problem. :slight_smile:

I don’t see anything in your GetACaptus class requiring something in that namespace. The code accesses the CaptusSpawner object.

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms