I have two issues. 1) I can grab anything and 2) when I let go of those objects they just stay where I let go at, no gravity function after clicking… Here is my current code Unity 5
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class GrabAndDrop : MonoBehaviour {
GameObject grabbedObject;
float grabbedObjectSize;
GameObject GetMouseHoverObject(float range)
{
Vector3 position = gameObject.transform.position;
RaycastHit raycastHit;
Vector3 target = position + Camera.main.transform.forward * range;
if (Physics.Linecast(position, target, out raycastHit))
return raycastHit.collider.gameObject;
return null;
}
void TryGrabObject(GameObject grabObject)
{
if (grabObject == null)
return;
else
if (grabObject.tag == “movable”);
{
grabbedObject = grabObject;
grabbedObjectSize = grabObject.GetComponent().bounds.size.magnitude * 2;
}
}
void DropObject()
{
if (grabbedObject == null)
return;
if (grabbedObject.GetComponent () != null)
grabbedObject.GetComponent ().velocity = Vector3.zero;
grabbedObject = null;
}
void Update () {
if (Input.GetMouseButtonDown(1))
{
if (grabbedObject == null)
TryGrabObject(GetMouseHoverObject(5));
else
DropObject();
}
if (grabbedObject != null)
{
Vector3 newPosition = gameObject.transform.position+Camera.main.transform.forward*grabbedObjectSize;
grabbedObject.transform.position = newPosition;
}
}
}[/code]