Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question Using Raycasts to make crosshairs pick up & drop objects

Discussion in 'Scripting' started by markasuter, Oct 8, 2021.

  1. markasuter

    markasuter

    Joined:
    May 20, 2019
    Posts:
    22
    My github repo


    Objective: Using the mouse, place the crosshairs on the orange cube. Click to pick it up. You SHOULD be able to run around with it and it stay with you. You SHOULD be able to let go of the mouse button to drop the object (turn gravity back on).

    Issues:
    1) I can pick up the object, and turn around and the orange object stays in front of me, but not if I move the player. It leaves the cube behind.
    2) Letting go of the mouse does not turn gravity back on, so it floats.

    I followed this tutorial (By Jimmy Vegas) to pick up boxes with the mouse(which worked fine)
    I followed this tutorial to implement raycasting with the crosshairs, which somewhat works as the color of the object hit by the raycast and tagged as "selectable" does indeed change material color...but issues 1 and 2 above persist. This script uses an empty gameobject "SelectionManager" that handles the raycasting.

    I'm fairly new to Raycasting and C#, so I'm trying to piece together different scripts...which could be the problem!
     
  2. Yoreki

    Yoreki

    Joined:
    Apr 10, 2019
    Posts:
    2,590
    I'm afraid few, if any, people are going to look through 20 minutes of tutorial to see which code you tried to implement, without even a guarantee that that's what you actually did, or being able to see what you might have changed.

    So the least you should do is post your code as is.
    There is several ways to achieve this, and we basically cant know what you did.

    Edit: I just noticed the github link above the image. Quite easy to miss.
    And those are quite a few scripts to look through.
     
  3. markasuter

    markasuter

    Joined:
    May 20, 2019
    Posts:
    22
    Sorry, here's my script on the empty object "SelectionManager".

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Suter_SelectionMgr : MonoBehaviour
    6. {
    7.     public Transform theDest;
    8.     public GameObject Destination;
    9.     private bool isHeld = false;
    10.     public Material highlightMaterial;
    11.     public Material defaultMaterial;
    12.     private string selectableTag = "Selectable";
    13.     private GameObject selectedObject;
    14.  
    15.     private Transform _selection;
    16.  
    17.     // Update is called once per frame
    18.     void Update()
    19.     {
    20.         if(_selection != null)
    21.         {
    22.             var selectionRenderer = _selection.GetComponent<Renderer>();
    23.             selectionRenderer.material = defaultMaterial;
    24.             _selection = null;
    25.         }
    26.         var ray = Camera.main.ScreenPointToRay(Input.mousePosition);
    27.         RaycastHit hit;
    28.         if(Physics.Raycast(ray, out hit))
    29.         {
    30.             var selection = hit.transform;
    31.             if (selection.CompareTag(selectableTag))
    32.             {
    33.                 //if raycast hit and Left mouse button is held down:
    34.                 if (Input.GetMouseButton(0))
    35.                 {
    36.                     selectedObject = hit.collider.gameObject;
    37.                     Debug.Log("gameObject: " + selectedObject + "is held.");
    38.                     isHeld = true;
    39.                     selectedObject.gameObject.GetComponent<BoxCollider>().enabled = false;
    40.                     selectedObject.gameObject.GetComponent<Rigidbody>().useGravity = false;
    41.                     hit.transform.position = theDest.position;
    42.                     hit.transform.parent = GameObject.Find("Destination").transform;
    43.                  
    44.                  
    45.                     Debug.Log("Pressed left click.");
    46.                     var selectionRenderer = selection.GetComponent<Renderer>();
    47.                     if (selectionRenderer != null)
    48.                     {
    49.                         Debug.Log("color change!");
    50.                         selectionRenderer.material = highlightMaterial;
    51.                     }
    52.                 }
    53.                 //if (Input.GetMouseButton(0) == false && isHeld == true)
    54.                 if (Input.GetMouseButtonUp(0))
    55.                 {
    56.                     isHeld = false;
    57.  
    58.                     Debug.Log("Let go of gameObject: " + selectedObject);
    59.                  
    60.                
    61.                 }
    62.  
    63.                 if (!isHeld)
    64.                 {
    65.                     Debug.Log("SELECTED OBJECT: " + selectedObject);
    66.                     hit.transform.parent = null;
    67.                     selectedObject.gameObject.GetComponent<Rigidbody>().useGravity = true;
    68.                     Debug.Log("Mouse UP");
    69.                     selectedObject.gameObject.GetComponent<BoxCollider>().enabled = true;
    70.                 }
    71.  
    72.                 _selection = selection;
    73.             }
    74.            
    75.         }
    76.     }
    77.  
    78.     void LateUpdate()
    79.     {
    80.         if (isHeld)
    81.         {
    82.             transform.position = Destination.transform.position;
    83.             //Debug.Log("transform.positon: " + transform.position + "...MainCamera1 position: " + MainCamera1.transform.position + "...offset: " + offset);
    84.         }
    85.  
    86.     }
    87. }
    88.  
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,713
    This is actually a great approach to expanding your brain. But it requires careful attention to detail and data flow to really find out what is going on. Fortunately there are some great timeless techniques to help you.

    Regardless of what is going on above, you must find a way to get the information you need in order to reason about what the problem is.

    What is often happening in these cases is one of the following:

    - the code you think is executing is not actually executing at all
    - the code is executing far EARLIER or LATER than you think
    - the code is executing far LESS OFTEN than you think
    - the code is executing far MORE OFTEN than you think
    - the code is executing on another GameObject than you think it is

    To help gain more insight into your problem, I recommend liberally sprinkling Debug.Log() statements through your code to display information in realtime.

    Doing this should help you answer these types of questions:

    - is this code even running? which parts are running? how often does it run? what order does it run in?
    - what are the values of the variables involved? Are they initialized? Are the values reasonable?
    - are you meeting ALL the requirements to receive callbacks such as triggers / colliders (review the documentation)

    Knowing this information will help you reason about the behavior you are seeing.

    You can also put in Debug.Break() to pause the Editor when certain interesting pieces of code run, and then study the scene

    You could also just display various important quantities in UI Text elements to watch them change as you play the game.

    If you are running a mobile device you can also view the console output. Google for how on your particular mobile target.

    Here's an example of putting in a laser-focused Debug.Log() and how that can save you a TON of time wallowing around speculating what might be going wrong:

    https://forum.unity.com/threads/coroutine-missing-hint-and-error.1103197/#post-7100494