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.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ObjectInteraction : MonoBehaviour
{
[SerializeField] Camera cam;
[SerializeField] float maxGrabDistance = 10f, throwForce = 20f;
public float lerpSpeed = 10f; // Changed to public
[SerializeField] Transform objectHolder;
Rigidbody grabbedRB;
void Update()
{
if (grabbedRB)
{
grabbedRB.MovePosition(Vector3.Lerp(grabbedRB.position, objectHolder.transform.position, Time.deltaTime * lerpSpeed));
if (Input.GetMouseButtonDown(0))
{
grabbedRB.isKinematic = false;
grabbedRB.AddForce(cam.transform.forward * throwForce, ForceMode.VelocityChange);
grabbedRB = null;
}
}
if (Input.GetKeyDown(KeyCode.E))
{
if (grabbedRB)
{
grabbedRB.isKinematic = false;
grabbedRB = null;
}
else
{
RaycastHit hit;
Ray ray = cam.ViewportPointToRay(new Vector3(0.5f, 0.5f));
if (Physics.Raycast(ray, out hit, maxGrabDistance))
{
grabbedRB = hit.collider.gameObject.GetComponent<Rigidbody>();
if (grabbedRB)
{
grabbedRB.isKinematic = true;
}
}
}
}
}
}