void Update() { if (Input.GetMouseButtonDown(0)) { if (portrait.IsPlaying("Idle")) { portrait.StopAll(0.3f); } else { portrait.CrossFade("Idle", 0.3f); } } }
void Update() { //Calculate Touch isTouched = false; Vector2 touchPosScreen = Vector2.zero; if (Input.GetMouseButton(0)) { isTouched = true; touchPosScreen = Input.mousePosition; } else if (Input.touchCount == 1) { TouchPhase touchPhase = Input.GetTouch(0).phase; if (touchPhase == TouchPhase.Began || touchPhase == TouchPhase.Moved || touchPhase == TouchPhase.Stationary) { isTouched = true; touchPosScreen = Input.GetTouch(0).position; } } if (isTouched) { touchPos = targetCamera.ScreenToWorldPoint(new Vector3(touchPosScreen.x, touchPosScreen.y, portrait.transform.position.z)); } else { isTouchDown = true; } // Touch Event if (isTouched != isTouchedPrevFrame) { isTouchHead = false; isTouchBody = false; if (isTouched) { Ray touchRay = targetCamera.ScreenPointToRay(new Vector3(touchPosScreen.x, touchPosScreen.y, 1000.0f)); RaycastHit[] hits = Physics.RaycastAll(touchRay); if (hits != null && hits.Length > 0) { for (int i = 0; i < hits.Length; i++) { if (hits[i].collider == bodyArea) { isTouchBody = true; break; } if (hits[i].collider == headArea) { isTouchHead = true; break; } } } if (isTouchHead) { if (isTouchDown) { // Smile! if (!portrait.IsPlaying("Smile")) { portrait.CrossFade("Smile", 0.2f); portrait.CrossFadeQueued("Idle"); } } } else if (isTouchBody) { if (isTouchDown) { // Angry if (!portrait.IsPlaying("Angry")) { portrait.Play("Angry"); portrait.CrossFadeQueued("Idle", 0.2f); } } } } else { //Reset "Eye" Timer isEyeReturn = true; eyeReturnTime = 0.0f; } } //Eye/Head Control if (isTouched) { Vector2 head2Touch = new Vector2(touchPos.x - headCenter.position.x, touchPos.y - headCenter.position.y); Vector2 eyeParam = new Vector2(Mathf.Clamp(head2Touch.x / eyeAreaMaxSize.x, -1, 1), Mathf.Clamp(head2Touch.y / eyeAreaMaxSize.y, -1, 1)); Vector2 headParam = new Vector2(Mathf.Clamp(head2Touch.x / headAreaMaxSize.x, -1, 1), Mathf.Clamp(head2Touch.y / headAreaMaxSize.y, -1, 1)); portrait.SetControlParamVector2(controlParamName_EyeDirection, eyeParam); portrait.SetControlParamVector2(controlParamName_HeadDirection, headParam); lastEyeParam = eyeParam; lastHeadParam = headParam; if (!touchParticle.isPlaying) { touchParticle.Play(); } handGroup.position = new Vector3(touchPos.x, touchPos.y, handGroup.position.z); handMesh_Released.enabled = false; handMesh_Pressed.enabled = true; } else { if (touchParticle.isPlaying) { touchParticle.Stop(); } if (Input.mousePresent) { Vector2 mousePosW = targetCamera.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, portrait.transform.position.z)); handGroup.position = new Vector3(mousePosW.x, mousePosW.y, handGroup.position.z); handMesh_Released.enabled = true; handMesh_Pressed.enabled = false; } else { handMesh_Released.enabled = false; handMesh_Pressed.enabled = false; } if (isEyeReturn) { eyeReturnTime += Time.deltaTime; if (eyeReturnTime < eyeReturnTimeLength) { apControlParam controlParam_Eye = portrait.GetControlParam(controlParamName_EyeDirection); apControlParam controlParam_Head = portrait.GetControlParam(controlParamName_HeadDirection); float itp = 1.0f - (eyeReturnTime / eyeReturnTimeLength); portrait.SetControlParamVector2(controlParamName_EyeDirection, lastEyeParam * itp + controlParam_Eye.Vector2ValueWithoutEditing * (1 - itp)); portrait.SetControlParamVector2(controlParamName_HeadDirection, lastHeadParam * itp + controlParam_Head.Vector2ValueWithoutEditing * (1 - itp)); } else { isEyeReturn = false; eyeReturnTime = 0.0f; } } } isTouchedPrevFrame = isTouched; if (isTouchDown && isTouched) { isTouchDown = false; } }