I’ve made a script that in theory is supposed to do this all if the object localScale is less than 0.4:
////
Detach the Key object from the player (the player’s GrabOn=false) when it hits the trigger for the first time and get rid of its gravity
Move the Key object toward the center of the trigger and slowly rotate it
Stop moving the Key object toward the center if the Key object is clicked again and allow the key to be reattached (player’s GrabOn=true)
////
This seems to work pretty much all the time, except sometimes while playing the game, everything the script is supposed to do doesn’t. I have to start the game over again for the code to work the same. Here’s the code:
Oh sorry, that’s in the telekinesis script. Maybe I should be more specific. I drag the object into the field of the trigger, and sometimes nothing happens, but most of the time it gets sucked toward the center like it’s supposed to. When it doesn’t work, it continues not to work until I restart the game.
public class telekinesis : MonoBehaviour
{
RaycastHit hit;
Ray ray;
public Transform Target;
public Transform CamPos;
RaycastHit TargetMem;
Grabee GrabScr;
public bool GrabOn;
public Vector3 WhereTo;
Vector3 MousePoint;
public float AddZ;
public float Speed=5;
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);
GrabScr = Target.GetComponent<Grabee>();
}
}
}
}
//actually moving the f***ing object
if (GrabOn)
{
MousePoint = new Vector3(Screen.width/2, Screen.height/2, AddZ);
WhereTo = Camera.main.ScreenToWorldPoint(MousePoint);
//moving time
TargetMem.rigidbody.velocity = (WhereTo-Target.position)*Speed;
if (!Physics.Raycast(new Vector3(Target.position.x,Target.position.y,Target.position.z), CamPos.forward, 1) Input.GetAxis("Mouse ScrollWheel") > 0)
{
AddZ += 1;
}
else if (!Physics.Raycast(new Vector3(Target.position.x,Target.position.y,Target.position.z), -CamPos.forward, 1) Input.GetAxis("Mouse ScrollWheel") < 0)
{
AddZ -= 1;
}
else if ((Physics.Raycast(new Vector3(Target.position.x,Target.position.y,Target.position.z), -CamPos.forward, 1) Input.GetAxis("Mouse ScrollWheel") < 0) || (Physics.Raycast(new Vector3(Target.position.x,Target.position.y,Target.position.z), CamPos.forward, 1) Input.GetAxis("Mouse ScrollWheel") > 0))
{
AddZ = Vector3.Distance(Target.position, transform.position);
}
if (Input.GetKey(KeyCode.LeftShift))
AddZ += .05F;
else if (Input.GetKey(KeyCode.LeftControl))
AddZ -= .05F;
if (Input.GetKey(KeyCode.C))
Target.localScale -= new Vector3(.005F,.005F,.005F);
else if (Input.GetKey(KeyCode.V) !GrabScr.BeinHit)
Target.localScale += new Vector3(.005F,.005F,.005F);
//Making some pretty limits
if (AddZ < 2.5F)
{
AddZ = 2.5F;
}
if (Target.localScale.x > 2.7F)
{
Target.localScale = new Vector3(2.7F,2.7F,2.7F);
}
if (Target.localScale.x < .4F)
{
Target.localScale = new Vector3(.4F,.4F,.4F);
}
}
}
}