Here is a script on picking items up in C#, but it does not work because it says that
“Assets/PickUpAble.cs(26,97): error CS0019: Operator +' cannot be applied to operands of type UnityEngine.Vector3’ and float'", "Assets/PickUpAble.cs(26,48): error CS1502: The best overloaded method match for UnityEngine.Vector3.Lerp(UnityEngine.Vector3, UnityEngine.Vector3, float)’ has some invalid arguments” and “Assets/PickUpAble.cs(26,48): error CS1503: Argument #2' cannot convert object’ expression to type UnityEngine.Vector3'" and "Assets/PickUpAble.cs(53,17): error CS0103: The name carriedobject’ does not exist in the current context”
I got this script of a video.
using UnityEngine;
using System.Collections;
public class PickUpAble : MonoBehaviour {
public float distance;
public float smooth;
GameObject mainCamera;
bool carrying;
GameObject carriedObject;
void Start () {
mainCamera = GameObject.FindWithTag("MainCamera");
}
// Update is called once per frame
void Update () {
if(carrying) {
carry(carriedObject);
} else {
pickup();
}
}
void carry(GameObject o) {
o.transform.position = Vector3.Lerp (o.transform.position, mainCamera.transform.forward + distance, Time.deltaTime + smooth);
}
void pickup () {
if(Input.GetKeyDown (KeyCode.E)) {
int x = Screen.width / 2;
int y = Screen.height / 2;
Ray ray = mainCamera.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.rigidbody.isKinematic = true;
}
}
}
}
void checkDrop() {
if (Input.GetKeyDown (KeyCode.E)) {
dropObject ();
}
}
void dropObject() {
carrying = false;
carriedobject.gameObject.rigidbody.isKinematic = false;
carriedObject = null;
}
}