TaiZhouCangChu_VRanime/Assets/动画/Scripts/AnimationControl.cs

69 lines
1.5 KiB
C#

using UnityEngine;
public class AnimationControl : MonoBehaviour
{
public GameObject bObject;
private Animator animator;
private bool isAnimation2Playing = false;
private Vector3 initialPosition;
private Quaternion initialRotation;
void Start()
{
animator = GetComponent<Animator>();
if (animator == null)
{
Debug.LogError("Animator component not found on the object");
}
initialPosition = transform.position;
initialRotation = transform.rotation;
PlayAnimation1();
// 使用repeatInterval变量作为时间间隔
}
private void PlayAnimation1()
{
animator.Play("Animation1");
}
void Update()
{
if (isAnimation2Playing && !animator.GetCurrentAnimatorStateInfo(0).IsName("Animation2"))
{
isAnimation2Playing = false;
animator.StopPlayback();
}
}
private void OnTriggerEnter(Collider other)
{
if (!isAnimation2Playing && other.gameObject == bObject)
{
PlayAnimation2();
}
}
private void PlayAnimation2()
{
animator.Play("Animation2");
isAnimation2Playing = true;
}
public void ResetToAnimation1AndInitialPosition()
{
animator.Play("Animation2", 0, 0.0f);
animator.Update(0);
animator.Play("Animation1");
isAnimation2Playing = false;
transform.position = initialPosition;
transform.rotation = initialRotation;
}
}