- Home /
Question by
Milda Koles · Mar 15, 2015 at 11:00 PM ·
fpsdragdrop
Drag and drop doesn't work properly
Hello, so I'm new to programming and I did drag and drop. But when I take and object to drag (fps) it appears very low, sometimes I can't even see how low and sometimes it appears so low that it falls through the floor. How can I solve this? My script:
using UnityEngine; using System.Collections;
public class DragAndDrop : 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 || !CanGrab(grabObject))
return;
grabbedObject = grabObject;
grabbedObjectSize = grabObject.GetComponent<Renderer>().bounds.size.magnitude;
}
bool CanGrab(GameObject canidate)
{
return canidate.GetComponent<Rigidbody> () != null;
}
void DropObject()
{
if (grabbedObject == null)
return;
if (grabbedObject.GetComponent<Rigidbody> () != null)
grabbedObject.GetComponent<Rigidbody> ().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;
}
}
}
Comment
When you drop it, adjust the y position of the object to be a little larger.
Your answer

Follow this Question
Related Questions
UI drag and drop 1 Answer
Simple drag and drop - no physics 4 Answers
How to drag and drop in 2D game 0 Answers
Pickup and move GameOjbect? 1 Answer
drag and drop 1 Answer