The code is supposed to let you pick up cubes by holding down your left mouse button, it works, but when you move your camera and let go, it doesn’t keep its momentum. Here is the code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Pickup_cube : MonoBehaviour {
//this decides when you are holding the cube
public bool holding;
void OnMouseDown()
{
holding = true;
}
void OnMouseUp()
{
holding = false;
}
void Update()
//this decides what the cube will do when you are holding it
{
if (Input.GetButtonDown ("Fire2")) {
}
if (Input.GetButtonUp ("Fire2")) {
}
if(holding == true)
{
this.transform.SetParent(Camera.main.transform);
Rigidbody Rigidbody = gameObject.GetComponentInParent<Rigidbody>( );
Rigidbody.useGravity = false;
Rigidbody.drag = 100;
Rigidbody.isKinematic = false;
}
if(holding == false)
{
this.transform.parent = null;
Rigidbody Rigidbody = gameObject.GetComponentInParent<Rigidbody>( );
Rigidbody.useGravity = true;
Rigidbody.drag = 0;
Rigidbody.isKinematic = false;
}
}
}