I just made a C# code that levitates objects if you click on it. It works perfectly, except when it tries to collide. I’m using Vector3.Lerp to make the levitation smooth, but Lerp directly controls the position of an object and so does Rigidbody. I want the box to collide, but my script is forcing it to go into the wall anyway. It looks like the box is having a seizure. Here’s the code (also attached):
public class telekinesis : MonoBehaviour
{
RaycastHit hit;
Ray ray;
Transform target;
RaycastHit TargetMem;
public bool GrabOn;
public Vector3 WhereTo;
Vector3 MousePoint;
public float AddZ;
public float Speed=10;
void Start ()
{
GrabOn=false;
}
void Update ()
{
//finding what the hell the mouse is clicking on
if (Input.GetMouseButtonDown(0))
{
ray = Camera.main.ScreenPointToRay(new Vector3(Screen.width/2,Screen.height/2,0));
if (GrabOn)
{
GrabOn = false;
TargetMem.rigidbody.useGravity = true;
}
else if (Physics.Raycast (ray, out hit))
{
if (!GrabOn)
{
if (hit.transform.tag == "Grabby")
{
target = hit.transform;
TargetMem = hit;
GrabOn = true;
hit.rigidbody.useGravity = false;
AddZ = Vector3.Distance(target.position, transform.position);
}
}
}
}
//actually moving the f***ing object
if (GrabOn)
{
MousePoint = new Vector3(Screen.width/2, Screen.height/2, AddZ);
WhereTo = Camera.main.ScreenToWorldPoint(MousePoint);
//enough of this defining sh**! Let's do this thang!
target.position = Vector3.Lerp(target.position,WhereTo,Vector3.Distance(target.position,WhereTo)/Speed);
AddZ += Input.GetAxis("Mouse ScrollWheel")*5;
//if you're a loser, and don't have a scroll wheel
if (Input.GetKey(KeyCode.LeftShift))
AddZ += .05F;
else if (Input.GetKey(KeyCode.LeftControl))
AddZ -= .05F;
//Making some pretty limits
if (AddZ < 2.5F)
{
AddZ = 2.5F;
}
}
}
}
I’ve tried putting in all kinds of ifs, collisions, and raycasts, but nothing seems to work. What’s the proper way to check for collisions in this circumstance? Thanks all in advance.
1379207–70099–$telekinesis.cs (1.47 KB)