private void OnTriggerEnter(Collider other) { if (fuse != null) { return; } Fuse f = other.gameObject.GetComponent <Fuse>(); if (f != null) { fuse = f; isMoving = true; t = 0.0f; // Ungrab si es un grabbable GrabableObj grabbable = fuse.gameObject.GetComponent <GrabableObj>(); if (grabbable != null) { Grabber hand = grabbable.GetGrabber(); if (hand != null) { hand.Ungrab(); // No es force ungrab porque no lo cogemos con la otra mano } } // Desactivar rigidbody (recordar que el ungrab lo activa) fuse.gameObject.GetComponent <Rigidbody>().isKinematic = true; // Impedir que pueda ser agarrado de nuevo GrabableObj gro = fuse.gameObject.GetComponent <GrabableObj>(); gro.CanBeGrabbed = false; gro.IsDistanceGrabbable = false; } }
private void OnTriggerEnter(Collider other) { PipeController pc = other.gameObject.GetComponent <PipeController>(); if (pc != null) { GrabableObj grabable = pc.gameObject.GetComponent <GrabableObj>(); // Puede ser ungrab porque lo puedes lanzar hacia el grid, no meterlo cogido pipeState[pc.id] = grabable.GetGrabber() != null ? GrabState.Grab : GrabState.Ungrab; insidePipes[pc.id] = pc; } }
private void OnTriggerExit(Collider other) { // Ver si el que ha salido es el fusible que tiene if (fuse != null && other.gameObject.GetInstanceID() == fuse.gameObject.GetInstanceID()) { GrabableObj gro = fuse.gameObject.GetComponent <GrabableObj>(); // Activar rigidbody solo si no se esta cogiendo if (gro.GetGrabber() == null) { fuse.gameObject.GetComponent <Rigidbody>().isKinematic = false; } gro.IsDistanceGrabbable = gro.DG; fuse = null; } }
void Update() { List <short> toRemove = new List <short>(); // loop over pipes inside foreach (KeyValuePair <short, PipeController> kv in insidePipes) { GrabableObj gro = kv.Value.gameObject.GetComponent <GrabableObj>(); // if change from grab to ungrab if (pipeState[kv.Key] == GrabState.Grab && gro.GetGrabber() == null) { pipeState[kv.Key] = GrabState.Grab2Ungrab; } if (pipeState[kv.Key] == GrabState.Grab2Ungrab) { // change pipe state to ungrab pipeState[kv.Key] = GrabState.Ungrab; // compute destinyP and destinyR Vector3 destinyP = GetTargetPosition(kv.Value.gameObject.transform.position); Quaternion destinyR = GetTargetRotation(kv.Value.gameObject.transform.rotation); // compute new occupied cells List <Vector3Int> newCells = GetNewCells(destinyP, destinyR, kv.Value.Positions, true); List <Vector3Int> newExits = GetNewCells(destinyP, destinyR, kv.Value.Exits, false); // if can fit if (CanFit(newCells)) { // set attached, destinyP and destinyR in PipeController kv.Value.Attach(destinyP, destinyR); // change also occupied cells setting the new occupied cells to the pipe id foreach (Vector3Int v in newCells) { occupiedCells[v.x, v.y, v.z] = kv.Key; } // move from insidePipes to attachedPipes with the pipe id attachedPipes[kv.Key] = insidePipes[kv.Key]; toRemove.Add(kv.Key); // do not delete immediatly because we are inside a loop // initialize new lists attachedPositions[kv.Key] = new List <Vector3Int>(); attachedExits[kv.Key] = new List <Vector3Int>(); // add the new occupied positions to attachedPositions with the pipe id foreach (Vector3Int v in newCells) { attachedPositions[kv.Key].Add(v); } // add the exits to attached exists with the pipe id foreach (Vector3Int v in newExits) { attachedExits[kv.Key].Add(v); } // set distance grab and kinematic to false gro.IsDistanceGrabbable = false; gro.gameObject.GetComponent <Rigidbody>().isKinematic = true; // Recompute correct paths Refresh(); } } } foreach (short s in toRemove) { insidePipes.Remove(s); } toRemove.Clear(); // loop over pipes attached foreach (KeyValuePair <short, PipeController> kv in attachedPipes) { GrabableObj gro = kv.Value.gameObject.GetComponent <GrabableObj>(); // if change from ungrab to grab if (pipeState[kv.Key] == GrabState.Ungrab && gro.GetGrabber() != null) { pipeState[kv.Key] = GrabState.Ungrab2Grab; } if (pipeState[kv.Key] == GrabState.Ungrab2Grab) { // change pipe state to grab pipeState[kv.Key] = GrabState.Grab; // set attached to false kv.Value.Detach(); // free occupied cells with -1 using attachedPostitions and pipe id foreach (Vector3Int v in attachedPositions[kv.Key]) { occupiedCells[v.x, v.y, v.z] = -1; } // move from attachedPipes to insidePipes insidePipes[kv.Key] = attachedPipes[kv.Key]; toRemove.Add(kv.Key); // do not delete immediatly because we are inside a loop // delete from attachedPositions attachedPositions.Remove(kv.Key); // delete from attachedExits attachedExits.Remove(kv.Key); // Reset distance grabbable to original value gro.IsDistanceGrabbable = gro.DG; // Recompute correct paths Refresh(); } } foreach (short s in toRemove) { attachedPipes.Remove(s); } }