Thursday, September 8, 2022
HomeGame Developmentunity - Holding "Shift" to lock mouse actions to a straight line?

unity – Holding “Shift” to lock mouse actions to a straight line?


Cache related x or y worth after shift key’s detected

PseudoCode:

Vector2 oldMousePos, mousePos
float x, y

Replace
 mousePos = mouse.place
 x = mousePos.x
 y = mousePos.y
 if ShiftKeyDown //fires on body shift key's first pressed
   //detect customers desired axis: x or y
   if (ABS(mousePos.x - oldMousePos.x) > ABS(mousePos.y - oldMousePos.y)) //person goes left to proper
     y = mousePos.y
   else 
     x = mousePos.x

// Do drawing right here or no matter, utilizing x & y coords

  oldMousePos = mousePos

This is a examined implementation (C#):

utilizing System.Collections;
utilizing System.Collections.Generic;
utilizing UnityEngine;

public class ShiftLine : MonoBehaviour
{
    [SerializeField] GameObject dice; //our voxel prefab
    Checklist<Vector2> occupiedVoxels; //to report crammed positions
    public enum LockedState
    {
        none,
        x,
        y
    }

    public LockedState state;

    void Begin()
    {
        occupiedVoxels = new Checklist<Vector2>();
        state = LockedState.none;
    }

    Vector2 oldMousePos, mousePos;
    float x, y;

    void Replace()
    {
        mousePos = Digicam.most important.ScreenToWorldPoint(Enter.mousePosition);

        if (Enter.GetKeyDown(KeyCode.RightShift))
        {
            // which manner are we shifting?
            if (Mathf.Abs(x - oldMousePos.x) > Mathf.Abs(y - oldMousePos.y))
            {
                y = oldMousePos.y;
                state = LockedState.y;
            }
            else
            {
                x = oldMousePos.x;
                state = LockedState.x;
            }
        }
        else if (Enter.GetKeyUp(KeyCode.RightShift))
        {
            state = LockedState.none;
        }

        if (Enter.GetMouseButton(0))
        {
            swap (state)
            {
                case LockedState.none:
                    x = mousePos.x;
                    y = mousePos.y;
                    break;

                case LockedState.x:
                    y = mousePos.y;
                    break;

                case LockedState.y:
                    x = mousePos.x;
                    break;
            }

            // spherical coordinates down 
            x = Mathf.Ground(x);
            y = Mathf.Ground(y);
            
           // is that this voxel place already occupied?
           // solely drop voxel if place is empty.

            if(!occupiedVoxels.Incorporates(new Vector2(x, y)))
            {
                occupiedVoxels.Add(new Vector2(x, y));
                var go = Instantiate(dice);
                go.rework.place = new Vector3(x, y, 0);   
            }
        }
        oldMousePos = mousePos;
    }
}

Demo:

Demo of mouse lock implementation

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments