Search Unity

Question Help with picking up items with key please

Discussion in 'Scripting' started by rickyluxius, May 6, 2023.

  1. rickyluxius

    rickyluxius

    Joined:
    Apr 17, 2023
    Posts:
    1
    Hi, I am trying to make it so my player picks up an item from the ground when i press the E key, at the moment, the player picks up the item automatically when ran over the item collider. I've been trying to figure out how to do it but I have had no success and end up messing things up, i have the "PlayerMove" script for the main player and a "Pickups" script that is attached to the items and detects when the player collides with them.

    PlayerMove Script:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.AI;
    5. using Cinemachine;
    6. public class PlayerMove : MonoBehaviour
    7. {
    8.     private NavMeshAgent nav;
    9.     private Animator anim;
    10.     private Ray ray;
    11.     private RaycastHit hit;
    12.     public GameObject aimObj;
    13.     private string axisNamed = "Mouse X";
    14.     public GameObject freeCam;
    15.     public GameObject staticCam;
    16.     private bool freeCamActive = true;
    17.  
    18.     private float x;
    19.     private float z;
    20.     private float velocitySpeed;
    21.     CinemachineTransposer ct;
    22.     CinemachineOrbitalTransposer cot;
    23.     private Vector3 pos;
    24.     private Vector3 currPos;
    25.  
    26.     public static bool canMove = true;
    27.     void Start()
    28.     {
    29.         nav = GetComponent<NavMeshAgent>();
    30.         anim = GetComponent<Animator>();
    31.         cot = staticCam.gameObject.GetComponent<CinemachineVirtualCamera>().GetCinemachineComponent<CinemachineOrbitalTransposer>();
    32.         freeCam.SetActive(false);
    33.         staticCam.SetActive(true);
    34.     }
    35.     void Update()
    36.     {
    37.         x = nav.velocity.x;
    38.         z = nav.velocity.z;
    39.         velocitySpeed = x + z;
    40.  
    41.         if (Input.GetMouseButton(0))
    42.         {
    43.             if(canMove == true)
    44.             {
    45.                 ray = Camera.main.ScreenPointToRay(Input.mousePosition);
    46.                 if (Physics.Raycast(ray, out hit))
    47.                 {
    48.                     nav.SetDestination(hit.point);  
    49.                 }
    50.             }
    51.         }
    52.  
    53.         if(nav.remainingDistance <= nav.stoppingDistance)
    54.         {
    55.             anim.SetBool("Sprinting", false);
    56.         }
    57.         else
    58.         {
    59.             anim.SetBool("Sprinting", true);
    60.         }
    61.  
    62.         pos = Input.mousePosition;
    63.  
    64.         if(Input.GetMouseButton(1))
    65.         {
    66.             if(Input.GetAxis("Mouse X") > 0)
    67.             {
    68.                 aimObj.transform.Rotate(0, 75 * Time.deltaTime, 0);
    69.             }
    70.             if(Input.GetAxis("Mouse X") < 0)
    71.             {
    72.                 aimObj.transform.Rotate(0, -75 * Time.deltaTime, 0);
    73.             }
    74.  
    75.             if (cot != null)
    76.             {
    77.                 cot.m_XAxis.m_InputAxisName = axisNamed;
    78.             }
    79.             if(pos.x != 0 || pos.y != 0)
    80.             {
    81.                 currPos = pos / 200;
    82.             }
    83.         }
    84.         else
    85.         {
    86.             if (cot != null)
    87.             {
    88.                 cot.m_XAxis.m_InputAxisName = null;
    89.                 cot.m_XAxis.m_InputAxisValue = 0;
    90.             }
    91.         }
    92.     }
    93. }
    Pickups Script:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Pickups : MonoBehaviour
    6. {
    7.     public int number;
    8.    
    9.     void OnTriggerEnter(Collider other)
    10.     {
    11.         if(other.CompareTag("Player"))
    12.         {
    13.             InventoryItems.newIcon = number;
    14.             InventoryItems.iconUpdate = true;
    15.             Destroy(gameObject);
    16.         }
    17.     }
    18. }