Skip to content

Instantly share code, notes, and snippets.

@ayuusweetfish
Created November 3, 2022 05:58
Show Gist options
  • Save ayuusweetfish/196ed7de2547e46917ae6a1a887f043a to your computer and use it in GitHub Desktop.
Save ayuusweetfish/196ed7de2547e46917ae6a1a887f043a to your computer and use it in GitHub Desktop.
Unity script for object pick-up with hand (2021.3.12f1)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class DragController : MonoBehaviour
{
public GameObject HandModel;
public Camera Camera;
public float MaxDetectDistance = 50;
public float GrabbedCloseUpDistance = 4;
public float IdleHandDistance = 1.5f;
public float HandAndObjectDistance = 1;
public LayerMask Layers = Physics.DefaultRaycastLayers;
public Vector3 DroppedBodiesPosOffset = new Vector3(-1.5f, -2, -1);
float handDist, handTargetDist;
Rigidbody target;
float hitDist;
Vector3 offs;
Vector3 origPos;
Quaternion origRot;
Vector3 origVel;
Vector3 origAngVel;
float targetDist;
List<Rigidbody> droppedBodies;
void Start() {
handDist = handTargetDist = IdleHandDistance;
if (Camera == null) {
Camera = Camera.main;
if (Camera == null) Debug.LogWarning("Please set a camera for DragController");
}
droppedBodies = new List<Rigidbody>();
}
void FixedUpdate() {
if (target != null) {
target.angularVelocity = target.angularVelocity.normalized *
(target.angularVelocity.magnitude * 0.97f);
targetDist -= (targetDist - GrabbedCloseUpDistance) * 0.18f;
handDist = targetDist - HandAndObjectDistance;
} else {
handDist -= (handDist - handTargetDist) * 0.18f;
}
Vector3 droppedTargetPos =
Camera.transform.TransformPoint(DroppedBodiesPosOffset);
foreach (Rigidbody body in droppedBodies) {
body.transform.position -=
(body.transform.position - droppedTargetPos) * 0.18f;
}
}
void Update() {
Ray ray = Camera.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
bool isHit = Physics.Raycast(ray.origin, ray.direction,
out hit, MaxDetectDistance, Layers);
if (isHit) {
handTargetDist = hit.distance - HandAndObjectDistance;
} else {
handTargetDist = IdleHandDistance;
}
if (Input.GetMouseButtonDown(0) && isHit) {
Vector3 ptr = ray.origin + ray.direction * hit.distance;
target = hit.rigidbody;
hitDist = hit.distance;
offs = target.transform.position - ptr;
targetDist = hitDist;
origPos = target.transform.position;
origRot = target.transform.rotation;
origVel = target.velocity;
origAngVel = target.angularVelocity;
target.isKinematic = true;
} else if (target != null) {
Vector3 ptr = ray.origin + ray.direction * targetDist;
Vector3 move = ptr + offs - target.transform.position;
target.transform.position += move;
if (Input.GetMouseButtonUp(0)) {
droppedBodies.Add(target);
target = null;
}
}
HandModel.transform.position = ray.origin + ray.direction * handDist;
}
}
/*
Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment