Picking up and carrying objects
I am trying to pick up an object and spawn it to an empty gameobject in front of my player, but its not working it doesnt even show the print in the console
{
private bool isHolding;
public GameObject spawnTo;
RaycastHit hit;
// Use this for initialization
void Start()
{
}
// Update is called once per frame
void FixedUpdate()
{
if (Input.GetMouseButtonDown(0))
{
if (Physics.Raycast(this.transform.position, transform.forward, out hit, 5.0f))
{
if (hit.collider.tag == "Moveable" && isHolding == false)
{
print("hit moveable object");
//hit.transform.gameObject.transform.position = spawnTo.transform.position;
hit.transform.gameObject.transform.parent = spawnTo.gameObject.transform;
hit.transform.gameObject.GetComponent<Rigidbody>().isKinematic = false;
isHolding = true;
// spawnTo
}
}
Debug.DrawRay(this.transform.position, transform.forward*5.0f, Color.red);
if (Input.GetMouseButtonDown(0) && isHolding == true)
{
hit.transform.gameObject.transform.parent = null;
hit.transform.gameObject.GetComponent<Rigidbody>().isKinematic = true;
isHolding = false;
}
}
}
}
i could pick them up if i changed the second mousebuttondown to mousebuttonUp but i couldnt release it, and after a while the cube just dissapeared. my console still didnt print it though. thanks in advance
Put your mouse button request in Update not in fixed update, because the mous buttens get set on the end of every frame.
Answer by CharlieLama · May 30, 2016 at 08:45 AM
Thanks, I didn't know that, But now i did it like this :
void FixedUpdate() {
if (Input.GetMouseButtonDown(0))
{
if (Physics.Raycast(this.transform.position, transform.forward, out hit, 5.0f))
{
PickUp();
}
Debug.DrawRay(this.transform.position, transform.forward*5.0f, Color.red);
}
}
and i put both my other if's in void PickUp, but it still doesnt work, it still doesnt even print my, hit moveable object, how? The ray can still detect the object, right?
I fixed it, it was because i clicked to pick it up and then immediately drop it again this now works;
{ public bool isHolding; public GameObject spawnTo; public GameObject camera;
RaycastHit hit;
// Update is called once per frame
void Update()
{
if (Input.Get$$anonymous$$ouseButtonDown(0) && isHolding)
Drop();
else if (Input.Get$$anonymous$$ouseButtonDown(0) && !isHolding)
{
if (Physics.Raycast(camera.transform.position, camera.transform.forward, out hit, 20.0f))
{
Debug.DrawRay(this.transform.position, camera.transform.forward * 20.0f, Color.black);
if (hit.collider.tag == "$$anonymous$$oveable")
{
Debug.DrawRay(this.transform.position, camera.transform.forward * 20.0f, Color.blue);
PickUp();
}
}
else
{
Debug.DrawRay(this.transform.position, camera.transform.forward * 20.0f, Color.yellow);
}
}
//else Debug.DrawRay(this.transform.position, camera.transform.forward * 20.0f, Color.red);
}
void PickUp()
{
Debug.DrawRay(this.transform.position, camera.transform.forward * 20.0f, Color.green);
//print("hit moveable object");
// hit.transform.gameObject.transform.position = spawnTo.transform.position;
hit.transform.gameObject.transform.parent = spawnTo.gameObject.transform;
hit.transform.gameObject.GetComponent<Rigidbody>().is$$anonymous$$inematic = false;
isHolding = true;
// spawnTo
}
void Drop()
{
print("dropping object");
hit.transform.gameObject.transform.parent = null;
hit.transform.gameObject.GetComponent<Rigidbody>().is$$anonymous$$inematic = true;
isHolding = false;
}
}
thanks for taking a look though.