private void FixedUpdate() { switch (state) { case pickup_state.carrying_object: if (was_pickup_pressed) { drop_object(); } break; case pickup_state.constraining_object: //Transfering object to another grid. MatchThreeObject m3_object = carried_item.GetComponent <MatchThreeObject>(); if (m3_object.grid != placement_grid) { m3_object.RemoveMyself(); m3_object.grid = placement_grid; //m3_object.match3_grid = structure; m3_object.AddMyself(true); m3_object.transform.SetParent(placement_grid.transform); } item_rigidbody = carried_item.GetComponent <Rigidbody>(); item_rigidbody.constraints = RigidbodyConstraints.FreezeRotation | RigidbodyConstraints.FreezePositionX | RigidbodyConstraints.FreezePositionZ; state = pickup_state.no_object_targeted; carried_item = null; break; } }
void Start() { prev_velocities_kept = 2; previous_velocities = new Vector3[prev_velocities_kept]; audio_src_ = GetComponent <AudioSource>(); rb = GetComponent <Rigidbody>(); weight_ = rb.mass; player = GameObject.FindGameObjectsWithTag("Player")[0]; player_pickup = player.GetComponent <PickUp>(); p_state = pickup_state.inactive; text_offset = new Vector3(0f, 0.5f, 0f); /* * text_obj = new GameObject(); * text_obj.AddComponent<TextMesh>(); * TextMesh t_mesh = text_obj.GetComponent<TextMesh>(); * t_mesh.characterSize = 1; * t_mesh.anchor = UnityEngine.TextAnchor.MiddleCenter; * t_mesh.alignment = UnityEngine.TextAlignment.Center; * t_mesh.text = "-F to pickup " + name_ + "-"; * * text_obj.transform.localScale = new Vector3(0.2f,0.2f,0.2f); * text_obj.transform.parent = transform; * text_obj.transform.localPosition = text_offset; * * * text_obj.SetActive(false); */ }
private void drop_object() { RoomResetterInteraction.canReset = true; carried_item.SetActive(true); //Remove constraints of rigidbody item_rigidbody.constraints = RigidbodyConstraints.None; item_rigidbody.MovePosition(finalDropLocation); //Reapply constraints on the next frame state = pickup_state.constraining_object; was_pickup_pressed = false; }
public void pickup_object(GameObject obj) { RoomResetterInteraction.canReset = true; state = pickup_state.carrying_object; carried_item = obj; carried_item_mesh = carried_item.GetComponent <MeshFilter>().mesh; carried_item_material = carried_item.GetComponent <MeshRenderer>().sharedMaterials[1]; item_rigidbody = carried_item.GetComponent <Rigidbody>(); // Debug.Log("Object carried: " + carried_item.name); carried_item.SetActive(false); }
void OnTriggerExit(Collider collision) { if (collision.gameObject.tag == "Player") { //print("TriggerEXIT"); // display text if the player is looking at the GameObject // //print(player_pickup); //player_pickup.addToInventory(gameObject, name_); p_state = pickup_state.inactive; } // disable the text }
void OnTriggerEnter(Collider collision) { if (collision.gameObject.tag == "Player") { //print("TriggerEnter"); // display text if the player is looking at the GameObject // //print(player_pickup); //player_pickup.addToInventory(gameObject, name_); if (previous_velocities[0].magnitude < 2f) { if (transform.parent != player.transform) { print("set active because of this collision"); p_state = pickup_state.active; } } } }
// Update is called once per frame void Update() { //Check for collision with liftable objects RaycastHit hit_info; Ray ray = new Ray(transform.position, transform.rotation * Vector3.forward); //Debug.DrawRay(ray.origin, ray.direction, Color.green); bool object_found = Physics.Raycast(ray, out hit_info, distance, layer_mask_pickup); switch (state) { case pickup_state.no_object_targeted: if (object_found && hit_info.transform.tag == "Interactable") { //Check the availability of the targeted object targeted_item = hit_info.transform.gameObject; targeted_item_interaction = targeted_item.GetComponent <Interaction>(); if (targeted_item_interaction.isBusy == false) { state = pickup_state.target_available; refresh_text(); } //Debug.Log("Target found."); //Debug.Log("Targeted item: " + targeted_item.name); } break; case pickup_state.target_available: if (!object_found || hit_info.transform.gameObject != targeted_item || targeted_item_interaction.isBusy == true) { //Object not targeted anymore //Debug.Log("Target lost."); e_button_prompt.fade_out_text(); state = pickup_state.no_object_targeted; } else { if (Input.GetButtonDown("Pickup")) { //Carry object targeted_item_interaction.interact(this); /* * state = pickup_state.carrying_object; * carried_item = targeted_item; * carried_item_mesh = carried_item.GetComponent<MeshFilter>().mesh; * carried_item_material = carried_item.GetComponent<MeshRenderer>().material; * item_rigidbody = carried_item.GetComponent<Rigidbody>(); * Debug.Log("Object carried: " + carried_item.name); * * carried_item.SetActive(false); */ } } break; case pickup_state.carrying_object: //Set initial drop location Vector3 initDropLocation = new Vector3(); if (object_found) { initDropLocation = hit_info.point + (hit_info.normal * 0.5f); } else { initDropLocation = transform.position + (transform.rotation * (Vector3.forward * distance)); } Vector3Int dropCoords = placement_grid.WorldToCell(initDropLocation); //Convert back to world space finalDropLocation = placement_grid.CellToWorld(dropCoords); bool can_drop = !Physics.CheckBox(finalDropLocation + new Vector3(0.5f, 0.55f, 0.5f), new Vector3(0.51f, 0.475f, 0.51f), Quaternion.identity, layer_mask_obstructed); bool show = !Physics.CheckBox(finalDropLocation + new Vector3(0.5f, 0.55f, 0.5f), new Vector3(0.51f, 0.475f, 0.51f), Quaternion.identity, layer_mask_hide); can_drop = can_drop && show; MaterialPropertyBlock properties = new MaterialPropertyBlock(); if (show) { if (can_drop) { e_button_prompt.fade_in_text("PLACE"); properties.SetColor("_Color", new Color(0.7f, 0.89f, 1f, 0.75f)); } else { e_button_prompt.fade_out_text(); properties.SetColor("_Color", new Color(1f, 0.89f, 0.7f, 0.75f)); } Graphics.DrawMesh(carried_item_mesh, finalDropLocation, Quaternion.identity, carried_item_material, 0, GetComponent <Camera>(), 0, properties, false); } if (Input.GetButtonDown("Pickup") && can_drop) { //Set carried item down // Debug.Log(drop_coords); //Debug.Log("Object dropped: " + carried_item.name); //Debug.Log("Location: " + drop_location); // was_pickup_pressed = true; } break; } //if Input.GetButtonDown("Pickup"){ //} }