Search Unity

Resolved Grabbing system

Discussion in 'Scripting' started by MrDiamondDog, Sep 17, 2020.

  1. MrDiamondDog

    MrDiamondDog

    Joined:
    Aug 12, 2020
    Posts:
    20
    I want a grabbing system that will put the object in front of the camera, and keep it there until the user presses the grab button again. I have the raycast set up and everything, the only thing i need is an equation that puts it in front of the camera. i have tried many things, but nothing seems to work. can anayone help please?
     
  2. adamgolden

    adamgolden

    Joined:
    Jun 17, 2019
    Posts:
    1,555
    Untested:
    Vector3 worldPositionAhead = Camera.main.transform.forward * distanceFromCamera;


    Edit: plus camera position..
    Vector3 worldPositionAhead = Camera.main.transform.position + (Camera.main.transform.forward * distanceFromCamera);
     
  3. MrDiamondDog

    MrDiamondDog

    Joined:
    Aug 12, 2020
    Posts:
    20
    i tried it, but i don't know how to implement it into my current grab code. here is that code:

    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class SelectionManager : MonoBehaviour
    4. {
    5.     [SerializeField] private string selectableTag = "Selectable";
    6.     [SerializeField] private Material highlightMaterial;
    7.     [SerializeField] private Material defaultMaterial;
    8.  
    9.     private Transform _selection;
    10.  
    11.     public Transform camera;
    12.    
    13.     private void Update()
    14.     {
    15.         if (_selection != null)
    16.         {
    17.             var selectionRenderer = _selection.GetComponent<Renderer>();
    18.             selectionRenderer.material = defaultMaterial;
    19.             _selection = null;
    20.         }
    21.        
    22.         var ray = Camera.main.ScreenPointToRay(Input.mousePosition);
    23.         RaycastHit hit;
    24.         if (Physics.Raycast(ray, out hit))
    25.         {
    26.             var selection = hit.transform;
    27.             if (selection.CompareTag(selectableTag))
    28.             {
    29.                 var selectionRenderer = selection.GetComponent<Renderer>();
    30.                 if (selectionRenderer != null)
    31.                 {
    32.                     selectionRenderer.material = highlightMaterial;
    33.                 }
    34.  
    35.                 _selection = selection;
    36.  
    37.                 if (Input.GetKeyDown(KeyCode.E))
    38.                 {
    39.                     UnityEngine.Debug.Log("Block Grabbed");
    40.  
    41.                     //equation here
    42.                 }
    43.             }
    44.         }
    45.     }
    46. }
     
  4. adamgolden

    adamgolden

    Joined:
    Jun 17, 2019
    Posts:
    1,555
    you're setting selection to null on the frame after it's set. you'll want to keep a reference around, or maybe add a
    public Transform grabbed
    ; then where you have "equation here" you would put
    grabbed = selection;
    and somewhere else in Update (maybe at the bottom) add something like..
    Code (CSharp):
    1. if (grabbed != null) grabbed.position = Camera.main.transform.position + (Camera.main.transform.forward * 10.0f);
     
  5. MrDiamondDog

    MrDiamondDog

    Joined:
    Aug 12, 2020
    Posts:
    20
    would 'grabbed' be the object your grabbing?
     
  6. MrDiamondDog

    MrDiamondDog

    Joined:
    Aug 12, 2020
    Posts:
    20
    and actually, after a bit of experimenting, i got it to teleport to the right spot, except the only problem is that it only teleports once and then stops. any fix?

    current code:

    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class SelectionManager : MonoBehaviour
    4. {
    5.     [SerializeField] private string selectableTag = "Selectable";
    6.     [SerializeField] private Material highlightMaterial;
    7.     [SerializeField] private Material defaultMaterial;
    8.  
    9.     private Transform _selection;
    10.  
    11.     public Transform camera;
    12.     public bool grabbing;
    13.    
    14.     private void Update()
    15.     {
    16.         if (_selection != null)
    17.         {
    18.             var selectionRenderer = _selection.GetComponent<Renderer>();
    19.             selectionRenderer.material = defaultMaterial;
    20.             _selection = null;
    21.         }
    22.        
    23.         var ray = Camera.main.ScreenPointToRay(Input.mousePosition);
    24.         RaycastHit hit;
    25.         if (Physics.Raycast(ray, out hit))
    26.         {
    27.             var selection = hit.transform;
    28.             if (selection.CompareTag(selectableTag))
    29.             {
    30.                 var selectionRenderer = selection.GetComponent<Renderer>();
    31.                 if (selectionRenderer != null)
    32.                 {
    33.                     selectionRenderer.material = highlightMaterial;
    34.                 }
    35.  
    36.                 _selection = selection;
    37.  
    38.                 if (Input.GetKeyDown(KeyCode.E))
    39.                 {
    40.                     UnityEngine.Debug.Log("Block Grabbed");
    41.                     if (selection != null) selection.position = camera.position + (camera.forward * 3.0f);
    42.                 }
    43.             }
    44.         }
    45.        
    46.     }
    47. }
     
  7. adamgolden

    adamgolden

    Joined:
    Jun 17, 2019
    Posts:
    1,555
    The issue is this:
    Code (CSharp):
    1. private void Update()
    2.     {
    3.         if (_selection != null)
    4.         {
    5.             //[...]
    6.             _selection = null;
    The very next frame you don't have a reference anymore, because you set that to null. If you want to keep holding it in front of you, there needs to be a reference - that's why I said you should add "grabbed" and use that. Try this..
    Code (CSharp):
    1. using UnityEngine;
    2. public class SelectionManager : MonoBehaviour
    3. {
    4.     [SerializeField] private string selectableTag = "Selectable";
    5.     [SerializeField] private Material highlightMaterial;
    6.     [SerializeField] private Material defaultMaterial;
    7.     private Transform _selection;
    8.     private Transform _grabbed;
    9.     public Transform camera;
    10.     public bool grabbing;
    11.  
    12.     private void Update()
    13.     {
    14.         if (_selection != null)
    15.         {
    16.             var selectionRenderer = _selection.GetComponent<Renderer>();
    17.             selectionRenderer.material = defaultMaterial;
    18.             _selection = null;
    19.         }
    20.  
    21.       if (_grabbed != null)
    22.       {
    23.         if (Input.GetKeyDown(KeyCode.E))
    24.         {
    25.           Debug.Log("No longer grabbing");
    26.           _grabbed = null;
    27.         } else {
    28.           _grabbed.position = camera.position + (camera.forward * 3.0f);
    29.         }
    30.         return;
    31.       }
    32.      
    33.         var ray = Camera.main.ScreenPointToRay(Input.mousePosition);
    34.         RaycastHit hit;
    35.         if (Physics.Raycast(ray, out hit))
    36.         {
    37.             var selection = hit.transform;
    38.             if (selection.CompareTag(selectableTag))
    39.             {
    40.                 var selectionRenderer = selection.GetComponent<Renderer>();
    41.                 if (selectionRenderer != null)
    42.                 {
    43.                     selectionRenderer.material = highlightMaterial;
    44.                 }
    45.                 _selection = selection;
    46.                 if (Input.GetKeyDown(KeyCode.E))
    47.                 {
    48.                   _grabbed = _selection;
    49.                     UnityEngine.Debug.Log("Block Grabbed");
    50.                 }
    51.             }
    52.         }
    53.      
    54.     }
    55. }
     
  8. MrDiamondDog

    MrDiamondDog

    Joined:
    Aug 12, 2020
    Posts:
    20
    the code worked great! thank you so much.
     
    adamgolden likes this.