IEnumerator GoToCoroutine() { handState = RightHandState.GoingTo; //move to destination Vector3 moveDir = desPosition - transform.localPosition; float movedDist = 0; while (movedDist < moveDir.magnitude) { yield return(null); transform.localPosition = transform.localPosition + moveDir.normalized * Time.deltaTime * movingSpeed; movedDist += Time.deltaTime * movingSpeed; } transform.localPosition = desPosition; //interact RaycastHit2D hit = Physics2D.CircleCast(transform.position, effectRadius, Vector2.zero, 100, interactLayerMask); if (currentUsable) { currentUsable.Use(transform.position.x, transform.position.y, hit.collider == null?null: hit.collider.gameObject); currentUsable.transform.SetParent(null); currentUsable = null; } else if (hit.collider != null) { currentUsable = hit.collider.gameObject.GetComponent <UsableObject>(); if (currentUsable != null) { //obtain the usable currentUsable.Obtain(); currentUsable.transform.SetParent(this.transform); Vector3 localP = currentUsable.transform.localPosition; localP.x = 0; localP.y = 0; currentUsable.transform.localPosition = localP; pick.PlayOneShot(pickup, 0.6f); } else { //interact currentInteracable = hit.collider.gameObject.GetComponent <Interactable>(); if (currentInteracable != null) { currentInteracable.OnClickLeft.Invoke(); } } } handState = RightHandState.Dragging; }
private void UseObject() { foreach (Collider2D c in Physics2D.OverlapCircleAll(transform.position, UseObjectRadius)) { UsableObject u = c.GetComponent <UsableObject>(); if (u == null) { continue; } u.Use(this); } }
public bool Use() { //Se lanza un rayo hacia delante, sumando cierta altura para no lanzarlo desde el suelo Ray usingRay = new Ray(this.transform.position + rayOffset, this.transform.forward); //Debug para poder visualizar el rayo en el inspector Debug.DrawLine(this.transform.position + rayOffset, this.transform.position + rayOffset + this.transform.forward * useDistance, Color.red); RaycastHit hit; if (Physics.Raycast(usingRay, out hit, useDistance)) { if (!hit.collider.isTrigger) { objectInUse = hit.collider.gameObject.GetComponent<UsableObject>(); if (objectInUse != null) { onCooldown = true; Invoke("ResetCooldown", useCooldown); //Se obtiene la normal del HIT para saber que parte del objeto ha encontrado el usable. //Solo se permite usar los objetos desde la parte de atras ( un angulo de 180 respecto al frente ) Vector3 normalLocal = hit.transform.TransformDirection(hit.normal); float angle = Vector3.Angle(hit.normal, hit.transform.forward); if (objectInUse.canUse) { objectInUse.Use(); if (objectInUse.type.Equals(UsableTypes.Hold)) { isUsing = true; if(objectInUse.usePosition!=null) { this.transform.position = objectInUse.usePosition.position; Vector3 look = objectInUse.transform.position; look.y = transform.position.y; this.transform.LookAt(look); } else { Vector3 frontPosition = hit.point + GetComponent<CapsuleCollider>().radius * hit.normal; Vector3 lookPosition = hit.point; lookPosition.y = transform.position.y; frontPosition.y = transform.position.y; this.transform.position = frontPosition; this.transform.LookAt(lookPosition); } } } } } } return isUsing; }
void OnTriggerEnter2D(Collider2D otherObj) { if (otherObj.gameObject.tag == "Orb") { useableObject = otherObj.gameObject.GetComponent<Orb>(); useableObject.Use(this); useableObject = null; } else if (otherObj.gameObject.tag == "Shrine") { UsableObject shrine = otherObj.gameObject.GetComponent<Shrine>(); if (shrine == null) { shrine = otherObj.gameObject.GetComponent<EvilShrine>(); } useableObject = shrine; } }
IEnumerator UseAnimation(UsableObject usable, float posX, float posY) { Vector3 pos = new Vector3(); pos.x = posX; pos.y = posY; desPosition = transform.parent.InverseTransformPoint(pos); desPosition.z = defaultPosition.z; handState = LeftHandState.UsingTo; yield return(new WaitForSeconds(movingTime - lightUpTime / 2)); mask.SetActive(true); sprite.sprite = sprite1; yield return(new WaitForSeconds(lightUpTime / 2)); RaycastHit2D hit = Physics2D.Raycast(transform.position, Vector2.zero, 100, interactLayerMask); usable.Use(posX, posY, hit.collider == null?null: hit.collider.gameObject); handState = LeftHandState.UsingBack; yield return(new WaitForSeconds(lightUpTime / 2)); mask.SetActive(false); sprite.sprite = sprite0; yield return(new WaitForSeconds(movingTime - lightUpTime / 2)); handState = LeftHandState.None; }