Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice
  3. Dismiss Notice

Question Pick Up Item with Raycast

Discussion in 'Scripting' started by key-es-co, Oct 4, 2023.

  1. key-es-co

    key-es-co

    Joined:
    Nov 5, 2022
    Posts:
    5
    Hello,

    I've been stuck trying to get an item to be spotted by the Raycast, get picked up, turn the bool to true and remove the item from the game. I can get the Raycast to find my Item and Debug.Log calls on Line 30 so it is reading the first part of my script. The actual changing of the Bool and removing the item are not working and the crosshair is turning Red but on button press but i want it to turn red when it interacts with the item and then white when turning away or the item is picked up. Ive tried afew different ideas and my code is just getting messy.

    My Pick Up Script:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5.  
    6. public class onInteract : MonoBehaviour
    7. {
    8.     [SerializeField] private KeyCode PickUp = KeyCode.E;
    9.     public int distance;
    10.  
    11.     [SerializeField] private Image crosshair = null;
    12.     private bool isCrosshairActive;
    13.  
    14.     public GameObject Axe;
    15.     public bool _Axe = false;
    16.  
    17.     private void Update()
    18.     {
    19.         if (Input.GetKeyDown(PickUp))
    20.         {
    21.             RaycastHit hit;
    22.  
    23.             Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
    24.             Debug.DrawRay(ray.origin, ray.direction * distance, Color.red, 5, false);
    25.             if (Physics.Raycast(ray, out hit, distance))
    26.             {
    27.                 if (hit.collider.gameObject.name == "item")
    28.                 {
    29.                     CrosshairChange(true);
    30.                     Debug.Log("Item Hit");
    31.                 }
    32.                 isCrosshairActive = true;
    33.             }
    34.         }
    35.         else
    36.         {
    37.             if (isCrosshairActive)
    38.             {
    39.                 CrosshairChange(false);
    40.             }
    41.         }
    42.         Debug.DrawLine(this.transform.position, this.transform.position + this.transform.forward);
    43.     }
    44.  
    45.     void CrosshairChange(bool on)
    46.     {
    47.         if (on)
    48.         {
    49.             crosshair.color = Color.red;
    50.         }
    51.         else
    52.         {
    53.             crosshair.color = Color.white;
    54.             isCrosshairActive = false;
    55.         }
    56.     }
    57.  
    58.     public void onInteractAxe()
    59.     {
    60.         if (Input.GetKeyDown(PickUp) && GameObject.FindWithTag("Axe"))
    61.         {
    62.             _Axe = true;
    63.             Debug.Log("picked up Axe");
    64.             Destroy(GameObject.FindWithTag("Axe"));
    65.         }
    66.     }
     
  2. zulo3d

    zulo3d

    Joined:
    Feb 18, 2023
    Posts:
    1,100
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5.    
    6. public class onInteract : MonoBehaviour
    7. {
    8.     [SerializeField] private KeyCode PickUp = KeyCode.E;
    9.     public int distance;
    10.    
    11.     [SerializeField] private Image crosshair = null;
    12.     private bool isCrosshairActive;
    13.    
    14.     public GameObject Axe;
    15.     public bool _Axe = false;
    16.    
    17.     private void Update()
    18.     {
    19.         RaycastHit hit;
    20.         Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
    21.         if (Physics.Raycast(ray, out hit, distance))
    22.         {
    23.             if (hit.collider.gameObject.name=="item")
    24.             {
    25.                 crosshair.color=Color.red;
    26.                 if (Input.GetKeyDown(PickUp))
    27.                     Destroy(hit.transform.gameObject);
    28.             }
    29.         }
    30.         else
    31.             crosshair.color=Color.white;
    32.     }
    33. }
    34.  
     
  3. key-es-co

    key-es-co

    Joined:
    Nov 5, 2022
    Posts:
    5
    Hi,

    Thanks so much for your response. this has cleared afew of my issues but it still doesnt seem to set the bool to true. i tried adding _Axe = true; before the Destroy but it then made it so as soon as i walk into the item, it picks up and doesnt require a Key Press. still not setting a bool though
     
  4. zulo3d

    zulo3d

    Joined:
    Feb 18, 2023
    Posts:
    1,100
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5.    
    6. public class onInteract : MonoBehaviour
    7. {
    8.     [SerializeField] private KeyCode PickUp = KeyCode.E;
    9.     public int distance;
    10.    
    11.     [SerializeField] private Image crosshair = null;
    12.     private bool isCrosshairActive;
    13.    
    14.     public GameObject Axe;
    15.     public bool _Axe = false;
    16.    
    17.     private void Update()
    18.     {
    19.         RaycastHit hit;
    20.         Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
    21.         if (Physics.Raycast(ray, out hit, distance))
    22.         {
    23.             if (hit.collider.gameObject.name=="item")
    24.             {
    25.                 crosshair.color=Color.red;
    26.                 if (Input.GetKeyDown(PickUp))
    27.                 {
    28.                     _Axe = true;
    29.                     Destroy(hit.transform.gameObject);
    30.                 }
    31.             }
    32.         }
    33.         else
    34.             crosshair.color=Color.white;
    35.     }
    36. }
    37.  
     
  5. key-es-co

    key-es-co

    Joined:
    Nov 5, 2022
    Posts:
    5
    ohhh i see my mistake!

    Thank you so much :)