- Home /
Pickup Script, Lerping to position needs to stay.
Hello, so I'm working on a script that will let the player pickup objects and I want it to lerp the object to an empty game object. There are a few problems though:
Doesn't use the ObjectHolderPoint rotation, so when I pick up an object it stays on it's xyz rotation but just moves to the origin of ObjectHolderPoint.
Raytrace from camera doesn't work if I angle the camera down, clips with character controller/collider.
I can pickup one object and then click another object with the Pickable tag to put it down.
It doesn't lerp/move to the location at all, but if it did it still wouldn't follow the ObjectHolderPoint until it's put down.
using UnityEngine; using System.Collections;
public class Pickup : MonoBehaviour {
public GameObject ObjectHoldingPoint; public Camera MainCamera; public float PickupDistance = 3.0f; public float Smoother = 2.0f; private bool Holding; //The Object in the world tagged with private GameObject PickedObject; //private Vector3 ObjectHoldingPointVector; void Awake () { Holding = false; } void Update () { //ObjectHoldingPointVector = ObjectHoldingPoint.transform.position; if (Input.GetMouseButtonDown(0) && !Holding) { Interaction(); //print ("Interact"); } else if (Input.GetMouseButtonDown(0) && Holding) { Drop (); } } void Interaction () { Vector3 ObjectHoldingPointVector = ObjectHoldingPoint.transform.position; //Vector3 PickedObjectVector = PickedObject.transform.position; RaycastHit HitPoint; if (Physics.Raycast(MainCamera.transform.position, MainCamera.transform.forward, out HitPoint)) { if (HitPoint.distance <= PickupDistance && HitPoint.collider.tag == "Pickable") { PickedObject = HitPoint.collider.gameObject; Vector3 PickedObjectVector = PickedObject.transform.position; PickedObject.transform.position = Vector3.Lerp(PickedObjectVector, ObjectHoldingPointVector, Smoother * Time.deltaTime); //If I enable the line below it will move instantiously, but won't follow rotation or the ObjectHolderPoint until dropped. //PickedObject.transform.position = ObjectHoldingPointVector; Holding = true; //print ("Hit"); } else { Holding = false; } } } //Placeholder, don't want to mess with physics/collision right now. void Drop () { PickedObject.transform.position = new Vector3(0, 0, 0); Holding = false; } }
If anyone could push me in the right direction I'd be very thankful.