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

Feature Request Grabbing script error

Discussion in 'Scripting' started by Misko2356, Aug 31, 2023.

  1. Misko2356

    Misko2356

    Joined:
    Feb 13, 2022
    Posts:
    3
    Hello, i made an script for simple grabbing, but the grabbed object can go into other collisions, and i dont know what to do, also i couldt find any solution to make it so i can rotate the object while holding right click. Please, help somebody.


    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class ObjectInteraction : MonoBehaviour
    6. {
    7.     [SerializeField] Camera cam;
    8.     [SerializeField] float maxGrabDistance = 10f, throwForce = 20f;
    9.     public float lerpSpeed = 10f; // Changed to public
    10.     [SerializeField] Transform objectHolder;
    11.  
    12.     Rigidbody grabbedRB;
    13.  
    14.     void Update()
    15.     {
    16.         if (grabbedRB)
    17.         {
    18.             grabbedRB.MovePosition(Vector3.Lerp(grabbedRB.position, objectHolder.transform.position, Time.deltaTime * lerpSpeed));
    19.  
    20.             if (Input.GetMouseButtonDown(0))
    21.             {
    22.                 grabbedRB.isKinematic = false;
    23.                 grabbedRB.AddForce(cam.transform.forward * throwForce, ForceMode.VelocityChange);
    24.                 grabbedRB = null;
    25.             }
    26.         }
    27.  
    28.         if (Input.GetKeyDown(KeyCode.E))
    29.         {
    30.             if (grabbedRB)
    31.             {
    32.                 grabbedRB.isKinematic = false;
    33.                 grabbedRB = null;
    34.             }
    35.             else
    36.             {
    37.                 RaycastHit hit;
    38.                 Ray ray = cam.ViewportPointToRay(new Vector3(0.5f, 0.5f));
    39.                 if (Physics.Raycast(ray, out hit, maxGrabDistance))
    40.                 {
    41.                     grabbedRB = hit.collider.gameObject.GetComponent<Rigidbody>();
    42.                     if (grabbedRB)
    43.                     {
    44.                         grabbedRB.isKinematic = true;
    45.                     }
    46.                 }
    47.             }
    48.         }
    49.     }
    50. }
     
  2. AngryProgrammer

    AngryProgrammer

    Joined:
    Jun 4, 2019
    Posts:
    435
    Rigidbody is for physics, it's natural for it that he interacts with other physical objects and colliders. You can solve the problem in two ways.
    1. Your object doesn't have to be a Rigidbody so you can use game objects with Collider only.
    2. Your object has to use Rigidbody so you can turn it off in time of dragging. All functions on time of dragging must know that you dragging it (some kind of bool duringDrag), and when you release the object it turns on Rigidbody again. You do all operations on Collider with trigger only.
    Edit: Sorry you probably asked for something else, my bad.
     
  3. Misko2356

    Misko2356

    Joined:
    Feb 13, 2022
    Posts:
    3
    Hi so, i tried making rigidbody disable when im grabbing the object, but that didnt quite work. But still, thanks for help
     
  4. AngryProgrammer

    AngryProgrammer

    Joined:
    Jun 4, 2019
    Posts:
    435
    You must change the whole concept because you doing drag and drop on a Rigidbody component. If you turn off Rigidbody it just won't work.

    Look at Collider functions (you need a trigger on) like OnMouseDown (link).