unity中多个门的开关动画保持独立性
List<Animation> storeAnimation; public void Awake()
{ storeAnimation = new List<Animation>(); storeAnimation.Clear(); } private void Update() { //1. 获取鼠标点击位置 //创建射线;从摄像机发射一条经过鼠标当前位置的射线 Ray ray = firstCamera.ScreenPointToRay(Input.mousePosition); //发射射线 RaycastHit hitInfo = new RaycastHit(); if (Physics.Raycast(ray, out hitInfo)) { if (hitInfo.collider.name == "plantLeftDoor01" || hitInfo.collider.name == "plantLeftDoor02" || hitInfo.collider.name == "plantLeftDoor03" || hitInfo.collider.name == "plantLeftDoor04" || hitInfo.collider.name == "plantLeftDoor05" || hitInfo.collider.name == "plantLeftDoor06" || hitInfo.collider.name == "plantLeftDoor07" || hitInfo.collider.name == "plantLeftDoor08") { OpenOrCloseLeftDoor(hitInfo); } else if (hitInfo.collider.name == "plantRightDoor01" || hitInfo.collider.name == "plantRightDoor02" || hitInfo.collider.name == "plantRightDoor03" || hitInfo.collider.name == "plantRightDoor04" || hitInfo.collider.name == "plantRightDoor05" || hitInfo.collider.name == "plantRightDoor06" || hitInfo.collider.name == "plantRightDoor07" || hitInfo.collider.name == "plantRightDoor08") { OpenOrCloseRightDoor(hitInfo); } } } private void OpenOrCloseLeftDoor(RaycastHit hitInfo) { leftAnim = hitInfo.collider.transform.GetComponent<Animation>(); if (!storeAnimation.Contains(leftAnim))//说明第一次点击 { leftAnim["leftDoor"].speed = 1; leftAnim.Play("leftDoor"); storeAnimation.Add(leftAnim); } else { leftAnim["leftDoor"].time = leftAnim["leftDoor"].length; leftAnim["leftDoor"].speed = -1; leftAnim.Play("leftDoor"); storeAnimation.Remove(leftAnim); } } private void OpenOrCloseRightDoor(RaycastHit hitInfo) { rightAnim = hitInfo.collider.transform.GetComponent<Animation>(); if (!storeAnimation.Contains(rightAnim))//说明第一次点击 { rightAnim["rightDoor"].speed = 1; rightAnim.Play("rightDoor"); storeAnimation.Add(rightAnim); } else { rightAnim["rightDoor"].time = rightAnim["rightDoor"].length; rightAnim["rightDoor"].speed = -1; rightAnim.Play("rightDoor"); storeAnimation.Remove(rightAnim); } }