Hey guys,
I am making a game where the player has to pick up objects and drop them into a cauldron. The part where the player picks and drops objects great, but then when I try to place the object into the cauldron the position is way offset and I do not understand why. Here is the code:
#pragma strict
// Major variables declaration
var validHit : boolean = false; // boolean for turning on GUI
private var holdsItem : boolean = false; // check number of items in hand
private var clonedObject:Transform; // clone of the destroyed instance
private var changeState : boolean; // holds the state of mousebutton
function Update()
{
var pickPos: Vector3 = Vector3(0.4, -2, 0.2); // offset of picked item
var hit : RaycastHit; // The object that was hit
var ray : Ray = Camera.main.ScreenPointToRay(Vector3(Screen.width/2, Screen.height/2, Camera.main.nearClipPlane)); // The ray - Middle of the screen
// if the middle of the screen is perpendicular to the object
if (Physics.Raycast (ray, hit, 10) && (hit.collider.tag == "basic" || hit.collider.tag == "mixed_lvl1" || hit.collider.tag == "mixed_lvl2") && holdsItem == false) {
validHit = true; // display GUI message
SendMessage("OnGUI"); // send GUI status to script_GUIController
if(Input.GetMouseButtonUp(0) && !changeState) { // and the left mouse button is pressed and changeState is false
mousePressed(); // next click drops the item
validHit = false; // hide GUI message
Destroy(hit.collider.gameObject, 0); // destroy the hit object
clonedObject = Instantiate(hit.collider.transform, transform.position, Quaternion(0,0,0,0)); // instantiate the object to player's position
clonedObject.name = clonedObject.name.Substring(0, clonedObject.name.length - 7); // Remove (CLone) from object's name
clonedObject.parent = Camera.main.transform; // parent to camera
clonedObject.transform.localPosition = pickPos; // and place it in front of the camera
holdsItem = true; // changes state of item picked
}
}
else {
validHit = false; // hide GUI message;
SendMessage("OnGUI");
if(Input.GetMouseButtonUp(0) && changeState && Physics.Raycast ( ray, hit, 10 ) && hit.collider.name == "cauldron") {
clonedObject.parent = null; // unparent object
//This is the part that doesn't work
clonedObject.transform.position = GameObject.Find("cauldronRightSlot").transform.position;
clonedObject.transform.rotation = GameObject.Find("cauldronRightSlot").transform.rotation;
changeState = false; // next click picks the item
holdsItem = false; // changes state of item picked
}
}
Thanks in advance!