- Home /
Enable gravity of a clone on MouseUp?
Hi!
I've ben trying to solve this problem with no succes.
The idea is to do a RayCast and to create an Object.
Move it around with the collider and the gravity turned off (to find the disered position)
And on MouseUp Enable the collider and the gravity.
I want to create multiple blocks, so this is why its a clone, But I cant seem to manage the variables in the right order. Thank you for you help.
function Update () {
if (Input.GetKeyDown(KeyCode.G))
{
builing = !builing;
}
if (builing==true)
{
var clone : GameObject;
if (Input.GetMouseButtonDown(0))
{
var ray1 = Camera.main.ScreenPointToRay(Input.mousePosition);
var hit1 = RaycastHit(); //Create a RaycastHit struct to hold information about the raycast
if(Physics.Raycast(ray1, hit1))
{
clone = Instantiate(cube, transform.position, transform.rotation);
clone.transform.position = hit1.point + Vector3(0,5,0);
clone.gameObject.BoxCollider.enable = false;
clone.rigidbody.useGravity = false;
}
}
else if (Input.GetMouseButtonUp(0))
{
clone.gameObject.BoxCollider.enable = true;
clone.rigidbody.useGravity = true;
}
}
}
What is the problem with the code (Not getting to the block, etc.) ?
Answer by wijesijp · May 28, 2014 at 04:33 AM
I am also little confused what you want to do
But the problem may be your clone variable is a local variable inside update function
var clone : GameObject;
It should be a global variable to this class, so at GetMouseButtonDown you set it. Remember to set it to null at GetMouseButtonUp after processing it.
I hope this helps