// Use this for initialization void Start() { handState = RightHandState.None; pick = GetComponent <AudioSource> (); defaultPosition = transform.localPosition; mainCamera = Camera.main; }
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; }
IEnumerator GoBackCoroutine() { handState = RightHandState.GoingBack; //move to default Vector3 moveDir = defaultPosition - 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 = defaultPosition; handState = RightHandState.None; }