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; } }
// Agarra un objeto. Es llamado desde un GrabableObj public void Grab(GrabableObj grabbable) { if (grabbed != null) { Debug.Log("Ya hay un objeto agarrado!"); return; } grabbed = grabbable.gameObject; // Guardar posicion y rotacion relativa grabbedPositionOffset = grabbed.transform.position - transform.position; initialHandRotation = transform.rotation; grabbedRotationOffset = Quaternion.Inverse(transform.rotation) * grabbed.transform.rotation; // Desactivar fuerzas sobre el objeto cogido grabbed.GetComponent <Rigidbody>().isKinematic = true; // Cambiar el layer a objeto agarrado (no colisiona con el jugador) grabbed.layer = LayerMask.NameToLayer("grabbed"); position0 = transform.position; speed = Vector3.zero; // No queremos laser en una mano con algo cogido interacter.TurnOffLaser(); t = 0.0f; }
// BIG TODO cmprobar que cada vez que obtengo un componente, este exista void Update() { if (isMoving) { t += Time.deltaTime; if (t > 1.0f) { isMoving = false; // Al llegar a su destino se puede agarrar de nuevo GrabableObj gro = fuse.gameObject.GetComponent <GrabableObj>(); gro.CanBeGrabbed = true; } GameObject go = fuse.gameObject; go.transform.position = Vector3.Lerp(go.transform.position, transform.position, t); go.transform.rotation = Quaternion.Lerp(go.transform.rotation, transform.rotation, t); } // Si aun tenemos fusible y no esta cogido else if (fuse != null && fuse.GetComponent <GrabableObj>().GetGrabber() == null) { // Comprobar si se ha movido // En caso de ser asi, reactivar el movimiento automatico float movementP = Vector3.Distance(transform.position, fuse.gameObject.transform.position); float movementR = Mathf.Abs(Quaternion.Angle(transform.rotation, fuse.gameObject.transform.rotation)); bool moved = movementP > 0.05f || movementR > 0.5f; if (moved) { isMoving = true; t = 0.0f; GrabableObj gro = fuse.gameObject.GetComponent <GrabableObj>(); gro.CanBeGrabbed = false; gro.IsDistanceGrabbable = false; fuse.gameObject.GetComponent <Rigidbody>().isKinematic = true; } } }
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); } }