Hello, I would first like to say that I am fairly new to the unity environment, anyways, I have this c# script that lets me pick up objects and put them down, which is awesome and it works, the only problem is, the object that you’re holding can go through other objects, the terrain , other objects ect. I’m not sure what to do? Could anyone offer me some help? here is my code for my GrabAndDrop script
using UnityEngine;
using System.Collections;
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 || !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(0))
{
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;
}
}
}