Can't figure out how to achieve an effect like QUBE

I saw this game recently and really liked it and I thought I could try porting it to Unity3D.

I can’t figure out how to move the cubes to certain positions and not anywhere. Could you provide a C# script for it? Thanks in advance :slight_smile:

P.S. I know how to implement the blue blocks using rigidbody.AddForce. I want to know the implementation of the red blocks.

Usually i don’t write whole scripts for other people that are too lazy… In this case it’s actually so simple that it took only a few min.

public class RedCube : MonoBehaviour
{
    Collider m_Collider;
    Transform m_Transform;

    public float currentTarget = 0;
    public float max = 3;
    public float Speed = 1;

    public float current = 0;
    private float m_Offset;
	void Start ()
	{
        m_Collider = collider;
        m_Transform = transform;
        m_Offset = m_Transform.localPosition.x;
	}

    bool CheckClick()
    {
        Ray R = Camera.main.ScreenPointToRay(Input.mousePosition);   // mouse point
        // Ray R = Camera.main.ViewportPointToRay(Vector2.one*0.5f);   // screen center
        RaycastHit hit;
        return m_Collider.Raycast(R,out hit,10000);
    }
	
	void Update ()
	{
        if (Input.GetMouseButtonDown(0) && CheckClick())
            currentTarget = Mathf.Min(currentTarget+1,max);
        if (Input.GetMouseButtonDown(1) && CheckClick())
            currentTarget = Mathf.Max(currentTarget-1,0);
        
        if (current != currentTarget)
        {            
            current = Mathf.MoveTowards(current,currentTarget,Time.deltaTime*Speed);
            if (Mathf.Abs(current - currentTarget) < 0.01f)
            {
                current = currentTarget;
            }
            Vector3 pos = m_Transform.localPosition;
            pos.x = m_Offset + current;
            m_Transform.localPosition = pos;
        }
    }
}

I’ve tested the script without any FPS controller. Just with a fix camera so i changed the raycast to mouseposition instead of screencenter.