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

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

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

[ad_1]

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

PseudoCode:

Vector2 oldMousePos, mousePos
float x, y

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

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

  oldMousePos = mousePos

Here’s a examined implementation (C#):

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

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

    public LockedState state;

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

    Vector2 oldMousePos, mousePos;
    float x, y;

    void Update()
    {
        mousePos = Camera.fundamental.ScreenToWorldPoint(Input.mousePosition);

        if (Input.GetKeyDown(KeyCode.ProperShift))
        {
            // which means are we transferring?
            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 (Input.GetKeyUp(KeyCode.ProperShift))
        {
            state = LockedState.none;
        }

        if (Input.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.Floor(x);
            y = Mathf.Floor(y);
            
           // is that this voxel place already occupied?
           // solely drop voxel if place is empty.

            if(!occupiedVoxels.Contains(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

[ad_2]

LEAVE A REPLY

Please enter your comment!
Please enter your name here