Carried object through terrain
So i have script that allows me to carry objects but it also allows me to put objects under terrain. Objects shouldn't get through terrain.
Script that i'm using to carry objects.
using UnityEngine;
using System.Collections;
public class PickupObject : MonoBehaviour
{
GameObject mainCamera;
bool carrying;
GameObject carriedObject;
public float distance;
public float smooth;
float b;
void Start ()
{
mainCamera = GameObject.FindWithTag("MainCamera");
}
void Update ()
{
if(carrying)
{
carry(carriedObject);
checkDrop();
}
else
{
pickup();
}
}
void carry(GameObject o)
{
o.transform.position = Vector3.Lerp (o.transform.position, mainCamera.transform.position + mainCamera.transform.forward * distance, Time.deltaTime * smooth);
o.transform.rotation = Quaternion.Slerp(o.transform.rotation, o.transform.rotation, Time.deltaTime * 1000);
}
void pickup()
{
if(Input.GetMouseButtonDown(0))
{
int x = Screen.width / 2;
int y = Screen.height / 2;
Ray ray = mainCamera.GetComponent<Camera>().ScreenPointToRay(new Vector3(x,y));
RaycastHit hit;
if(Physics.Raycast(ray, out hit))
{
Pickupable p = hit.collider.GetComponent<Pickupable>();
if(p != null)
{
carrying = true;
carriedObject = p.gameObject;
p.gameObject.GetComponent<Rigidbody>().isKinematic = true;
}
}
}
}
void checkDrop()
{
if(Input.GetMouseButtonDown(0))
{
dropObject();
}
}
void dropObject()
{
carrying = false;
carriedObject.gameObject.GetComponent<Rigidbody>().isKinematic = false;
carriedObject = null;
}
}
Comment
Your answer
Follow this Question
Related Questions
How to know where my object is going to fall? 1 Answer
Object Reference Error 1 Answer
button change object position 0 Answers
Click Vector3 position to change color 0 Answers