Search Unity

Question Point and Click UI Menu

Discussion in 'Input System' started by tysongrunter, Mar 27, 2023.

  1. tysongrunter

    tysongrunter

    Joined:
    Mar 25, 2023
    Posts:
    19
    Okay, I am trying to code a game so that when you hit Ctrl, it toggles a menu on and off. When the menu is off, you can move around, jump, attack, etc, and when the menu is on, you point and click on things. How would I go about setting that up using the new event system? I have action maps both for the movement and for the point-and-click menu. Here's my code:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.InputSystem;
    5.  
    6. public class Player : MonoBehaviour
    7. {
    8.     //holds the rigidbody
    9.     [SerializeField]
    10.     Rigidbody rb;
    11.     //holds Kaitlyn's max speed while walking
    12.     [SerializeField]
    13.     float WalkSpeed;
    14.     //holds the direction Kaitlyn moves in
    15.     private Vector3 forceDirection = Vector3.zero;
    16.     //holds Kaitlyn's terminal velocity
    17.     [SerializeField]
    18.     float terminalVelocity;
    19.     //holds the player's controls
    20.     [SerializeField]
    21.     private PlayerControls controls;
    22.     //holds the movement
    23.     [SerializeField]
    24.     private InputAction move;
    25.     //holds the force Kaitlyn applies to herself while moving
    26.     [SerializeField]
    27.     private float movementForce;
    28.     //holds the camera
    29.     [SerializeField]
    30.     private Camera playerCamera;
    31.     //holds Kaitlyn's jump force
    32.     [SerializeField]
    33.     float JumpForce;
    34.     //holds the player's capsule collider
    35.     [SerializeField]
    36.     private CapsuleCollider capsule;
    37.     //holds the player's attack strength
    38.     public int strength;
    39.     //accesses the transform that Kaitlyn gives to the items she grabs
    40.     public Transform holdSpace;
    41.     //stores the rigidbody of the object picked up
    42.     [SerializeField]
    43.     Rigidbody _heldObject;
    44.     //communicates to the grabbable script
    45.     [SerializeField]
    46.     private Grabbable grabbable;
    47.     //stores the KaitlynSO
    48.     [SerializeField]
    49.     private KaitlynSO kaitlyn;
    50.     //stores the bool for checking if the game's paused
    51.     public bool IsPaused;
    52.  
    53.     //gets Kaitlyn's rigidbody, collider, and controls, while setting her HP to max
    54.     void Awake()
    55.     {
    56.         rb = GetComponent<Rigidbody>();
    57.         controls = new PlayerControls();
    58.         capsule = GetComponent<CapsuleCollider>();
    59.         kaitlyn.HP = kaitlyn.maxHP;
    60.         IsPaused = false;
    61.     }
    62.  
    63.     //enables Kaitlyn's moveset
    64.     private void OnEnable()
    65.     {
    66.         controls.Kaitlyn.Jump.started += DoJump;
    67.         controls.Kaitlyn.Crouch.started += DoCrouch;
    68.         controls.Kaitlyn.Crouch.canceled += DoStand;
    69.         controls.Kaitlyn.Sprint.started += DoSprint;
    70.         controls.Kaitlyn.Sprint.canceled += DoStand;
    71.         controls.Kaitlyn.Attack.started += DoAttack;
    72.         controls.Kaitlyn.Grab.started += DoGrab;
    73.         controls.Kaitlyn.Pause.started += DoPause;
    74.         controls.Menu.Pause.started += DoPause;
    75.         controls.Menu.Click.started += DoClick;
    76.         move = controls.Kaitlyn.Move;
    77.         controls.Kaitlyn.Enable();
    78.         controls.Menu.Enable();
    79.     }
    80.  
    81.     //disables Kaitlyn's moveset
    82.     private void OnDisable()
    83.     {
    84.         controls.Kaitlyn.Jump.started -= DoJump;
    85.         controls.Kaitlyn.Crouch.started -= DoCrouch;
    86.         controls.Kaitlyn.Crouch.canceled -= DoStand;
    87.         controls.Kaitlyn.Sprint.started -= DoSprint;
    88.         controls.Kaitlyn.Sprint.canceled -= DoStand;
    89.         controls.Kaitlyn.Attack.started -= DoAttack;
    90.         controls.Kaitlyn.Grab.started -= DoGrab;
    91.         controls.Kaitlyn.Pause.started -= DoPause;
    92.         controls.Menu.Pause.started -= DoPause;
    93.         controls.Menu.Click.started -= DoClick;
    94.         controls.Kaitlyn.Disable();
    95.         controls.Menu.Disable();
    96.     }
    97.  
    98.     //called once a frame
    99.     private void FixedUpdate()
    100.     {
    101.         //holds Kaitlyn's movement
    102.         forceDirection += move.ReadValue<Vector2>().x * GetCameraRight(playerCamera) * movementForce;
    103.         forceDirection += move.ReadValue<Vector2>().y * GetCameraForward(playerCamera) * movementForce;
    104.         //applies force to Kaitlyn
    105.         rb.AddForce(forceDirection, ForceMode.Impulse);
    106.         forceDirection = Vector3.zero;
    107.         //lets Kaitlyn fall with gravity
    108.         if(rb.velocity.y < 0f)
    109.         {
    110.             rb.velocity -= Vector3.down * Physics.gravity.y * Time.fixedDeltaTime;
    111.         }
    112.         //holds Kaitlyn's horizontal velocity
    113.         Vector3 horizontalVelocity = rb.velocity;
    114.         horizontalVelocity.y = 0;
    115.         //stops Kaitlyn from moving faster than her max speed
    116.         if(horizontalVelocity.sqrMagnitude > WalkSpeed * WalkSpeed)
    117.         {
    118.             rb.velocity = horizontalVelocity.normalized * WalkSpeed + Vector3.up * rb.velocity.y;
    119.         }
    120.         //calls the LookAt() function
    121.         LookAt();
    122.     }
    123.  
    124.     //bases Kaitlyn's X-axis off the camera's
    125.     private Vector3 GetCameraRight(Camera camera)
    126.     {
    127.         Vector3 right = camera.transform.right;
    128.         right.y = 0;
    129.         return right.normalized;
    130.     }
    131.  
    132.     //bases Kaitlyn's Z-axis off the camera's
    133.     private Vector3 GetCameraForward(Camera camera)
    134.     {
    135.         Vector3 forward = camera.transform.forward;
    136.         forward.y = 0;
    137.         return forward.normalized;
    138.     }
    139.  
    140.     //makes Kaitlyn turn in the direction she looks
    141.     private void LookAt()
    142.     {
    143.         Vector3 direction = rb.velocity;
    144.         direction.y = 0f;
    145.         if(move.ReadValue<Vector2>().sqrMagnitude > 0.1f && direction.sqrMagnitude > 0.1f)
    146.         {
    147.             rb.rotation = Quaternion.LookRotation(direction, Vector3.up);
    148.         }
    149.         else
    150.         {
    151.             rb.angularVelocity = Vector3.zero;
    152.         }
    153.     }
    154.  
    155.     //makes Kaitlyn jump, if she is touching the ground
    156.     private void DoJump(InputAction.CallbackContext obj)
    157.     {
    158.         //only lets Kaitlyn jump if she's in contact with the ground
    159.         if (IsGrounded())
    160.         {
    161.             forceDirection += Vector3.up * JumpForce;
    162.         }
    163.     }
    164.  
    165.     //checks if Kaitlyn is standing on solid ground
    166.     private bool IsGrounded()
    167.     {
    168.         //contains the ground check raycast's origin and direction
    169.         Ray ray = new Ray(this.transform.position + Vector3.up, Vector3.down);
    170.         //sets to true if the raycast hits something
    171.         if (Physics.Raycast(ray, out RaycastHit hit, 3f))
    172.         {
    173.             return true;
    174.         }
    175.         //sets to false if it doesn't hit
    176.         else
    177.         {
    178.             return false;
    179.         }
    180.     }
    181.  
    182.     //lets Kaitlyn crouch, reducing her height and top speed
    183.     private void DoCrouch(InputAction.CallbackContext obj)
    184.     {
    185.         capsule.height = 1.5f;
    186.         WalkSpeed = 1f;
    187.         movementForce = 1f;
    188.     }
    189.  
    190.     //lets Kaitlyn stand up straight after crouching or sprinting, resetting her height, top speed, and acceleration
    191.     private void DoStand(InputAction.CallbackContext obj)
    192.     {
    193.         capsule.height = 3f;
    194.         WalkSpeed = 2f;
    195.         movementForce = 1f;
    196.     }
    197.  
    198.     //allows Kaitlyn to run, increasing her top speed and acceleration
    199.     private void DoSprint(InputAction.CallbackContext obj)
    200.     {
    201.         WalkSpeed = 10f;
    202.         movementForce = 2.5f;
    203.     }
    204.  
    205.     //lets Kaitlyn attack and throw
    206.     private void DoAttack(InputAction.CallbackContext obj)
    207.     {
    208.         //checks if Kaitlyn's hands are empty
    209.         if(grabbable == null)
    210.         {
    211.             //sets the origin at the player's position and the direction at in front of Kaitlyn
    212.             Ray ray = new Ray(this.transform.position, this.transform.forward);
    213.             //checks if there's something 1.665 m in front of Kaitlyn
    214.             if (Physics.Raycast(ray, out RaycastHit hit, 1.665f))
    215.             {
    216.                 //holds the rigidbody of the grabbable object
    217.                 Rigidbody otherRB = hit.rigidbody;
    218.                 //checks if that thing is tagged as damageable, and if so, communicates to the damageable script
    219.                 if (hit.transform.gameObject.tag == "Damageable")
    220.                 {
    221.                     hit.transform.gameObject.SendMessage("TakeDamage", strength);
    222.                 }
    223.             }
    224.         }
    225.         //throws if Kaitlyn is holding something
    226.         else
    227.         {
    228.             grabbable.Throw();
    229.             grabbable = null;
    230.         }
    231.     }
    232.  
    233.     //lets Kaitlyn grab, carry, and drop objects
    234.     private void DoGrab(InputAction.CallbackContext obj)
    235.     {
    236.         //checks if Kaitlyn's holding something
    237.         if(grabbable == null)
    238.         {
    239.             //sets the origin at the player's position and the direction at in front of Kaitlyn
    240.             Ray ray = new Ray(this.transform.position, this.transform.forward);
    241.             //checks if there's something 1.665 m in front of Kaitlyn
    242.             if (Physics.Raycast(ray, out RaycastHit hit, 1.665f))
    243.             {
    244.                 //grabs if that object in front of Kaitlyn has a grabbable script
    245.                 if(hit.transform.TryGetComponent(out grabbable))
    246.                 {
    247.                     grabbable.Grab(holdSpace);
    248.                 }
    249.             }
    250.         }
    251.         //if Kaitlyn is holding something, she drops it
    252.         else
    253.         {
    254.             grabbable.Drop();
    255.             grabbable = null;
    256.         }
    257.     }
    258.  
    259.     //alternates between paused and unpaused
    260.     private void DoPause(InputAction.CallbackContext obj)
    261.     {
    262.         if(IsPaused == false)
    263.         {
    264.             IsPaused = true;
    265.             controls.Kaitlyn.Disable();
    266.             controls.Menu.Enable();
    267.         }
    268.         else
    269.         {
    270.             IsPaused = false;
    271.             controls.Kaitlyn.Enable();
    272.             controls.Menu.Disable();
    273.         }
    274.     }
    275.  
    276.     //clicks on buttons on the pause menu UI
    277.     private void DoClick(InputAction.CallbackContext obj)
    278.     {
    279.  
    280.     }
    281. }
    282.  
    How do I get the point-and-click menu to work?