According to the tutorial video, I created the code for lifting an object.
How do I make the object follow the camera as fast as the camera rotates? Or, at least, when the camera rotates fast, the object doesn’t fly away, and a new object doesn’t cling when there is already a lifted object.
(Pardon my English if you don’t understand)
public int GRABI;
public float grabPower = 10.0f;
public float throwPower = 10f; //speed push
public float RayDistance = 30.0f; //distanse
private bool Grab = false; //attraction function
private bool Throw = false; //push function
public Transform offset;
public GameObject cam;
RaycastHit hit; //ray
public float x;
private void Start()
{
GRABI = 0;
}
void Update()
{
Physics.Raycast(cam.transform.position, cam.transform.forward, out hit, RayDistance);
if (Input.GetKeyDown(KeyCode.E))
{
if (hit.rigidbody)
{
GRABI = GRABI + 1;
switch (GRABI)
{
case 1:
Grab = true;
break;
case 2:
Grab = false;
break;
default:
break;
}
if (GRABI == 3)
{
GRABI = 0;
}
if (Grab == false)
{
GRABI = 0;
}
}
Debug.Log(GRABI);
}
if (Input.GetMouseButtonDown(0))
{
if (Grab)
{
GRABI = 0;
}
}
if (Input.GetMouseButtonDown(0))
{//if the left mouse button is pressed
if (Grab)
{
Grab = false;
Throw = true;
}
}
if (Grab)
{//attraction function
if (hit.rigidbody)
{
hit.rigidbody.velocity = (offset.position - (hit.transform.position + hit.rigidbody.centerOfMass)) * grabPower * x;
}
}
if (Throw)
{//push function
if (hit.rigidbody)
{
hit.rigidbody.velocity = new Ray(cam.transform.position, cam.transform.forward).direction * throwPower;
Throw = false;
}
}
}