Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
4.1 kB
3
Indexable
Never
using System.Collections;
using System.Collections.Generic;
using DG.Tweening;
using EasyButtons;
using UnityEngine;

[ExecuteInEditMode]
public class TestAnima : MonoBehaviour
{
    [SerializeField] private AnimationCurve rotateXCurve;
    [SerializeField] private AnimationCurve rotateZCurve;

    [SerializeField]
    private Transform Attacker_pos;
    [SerializeField]
    private Transform Target_pos;
    
    public void AnimateAttack(bool isEnemy = false)
    {
        Vector3 direction = isEnemy
            ? Attacker_pos.position - Target_pos.position
            : Target_pos.position - Attacker_pos.position;
        Vector3 localDirectionToTarget = Attacker_pos.InverseTransformDirection(direction.normalized);

        Quaternion targetRotation = Quaternion.LookRotation(Vector3.forward, localDirectionToTarget);

        Vector3 originalRotation = Attacker_pos.rotation.eulerAngles;
        var targetXRotation = isEnemy ? -25f : 25;
        var targetZRotation = targetRotation.eulerAngles.z;

        // X tween
        Tween rotationXTween1 = DOTween.To(() => Attacker_pos.rotation.eulerAngles.x, UpdateRotateX, targetXRotation, 0.25f).SetEase(rotateXCurve).Pause();
        Tween rotationXTween2 = DOTween.To(() => Attacker_pos.rotation.eulerAngles.x, UpdateRotateX, 0, 0.025f).Pause();
        Tween rotationXTween3 = DOTween.To(() => Attacker_pos.rotation.eulerAngles.x, UpdateRotateX, targetXRotation * -1, 0.025f).Pause();
        Tween rotationXTween4 = DOTween.To(() => Attacker_pos.rotation.eulerAngles.x, UpdateRotateX, 0, 0.1f).Pause();
        
        Sequence rotationXSequence = DOTween.Sequence();
        rotationXSequence.AppendInterval(0.1f);
        rotationXSequence.Append(rotationXTween1);
        rotationXSequence.Append(rotationXTween2);
        rotationXSequence.Append(rotationXTween3);
        rotationXSequence.Append(rotationXTween4);
        rotationXSequence.Pause();
        
        // Y tween
        Tween rotationYTween1 = DOTween.To(() => Attacker_pos.rotation.eulerAngles.x, UpdateRotateY, -12, 0.1f).SetEase(rotateXCurve).Pause();
        Tween rotationYTween2 = DOTween.To(() => Attacker_pos.rotation.eulerAngles.x, UpdateRotateY, 0, 0.025f).Pause();

        Sequence rotationYSequence = DOTween.Sequence();
        rotationYSequence.AppendInterval(0.15f);
        rotationYSequence.Append(rotationYTween1);
        rotationYSequence.Append(rotationYTween2);
        rotationXSequence.Pause();
        
        // Z tween
        Tween rotationZTween1 = DOTween.To(() => Attacker_pos.rotation.eulerAngles.z, UpdateRotateZ, targetZRotation, 0.1f).SetEase(rotateZCurve).Pause();
        Tween rotationZTween2 = DOTween.To(() => Attacker_pos.rotation.eulerAngles.z, UpdateRotateZ, 0, 0.05f).SetEase(rotateZCurve).Pause();
        
        Sequence rotationZSequence = DOTween.Sequence();
        rotationZSequence.AppendInterval(0.05f);
        rotationZSequence.Append(rotationZTween1);
        rotationZSequence.AppendInterval(0.1f);
        rotationZSequence.Append(rotationZTween2);
        rotationZSequence.Pause();

        Sequence parallelSequence = DOTween.Sequence();
        parallelSequence.Append(rotationXSequence);
        parallelSequence.Join(rotationYSequence);
        parallelSequence.Join(rotationZSequence);
        parallelSequence.Play();
    }

    private void UpdateRotateX(float value)
    {
        Vector3 rotation = Attacker_pos.rotation.eulerAngles;
        rotation.x = value;
        Attacker_pos.rotation = Quaternion.Euler(rotation);
    }
    
    private void UpdateRotateY(float value)
    {
        Vector3 rotation = Attacker_pos.rotation.eulerAngles;
        rotation.y = value;
        Attacker_pos.rotation = Quaternion.Euler(rotation);
    }
    
    private void UpdateRotateZ(float value)
    {
        Vector3 rotation = Attacker_pos.rotation.eulerAngles;
        rotation.z = value;
        Attacker_pos.rotation = Quaternion.Euler(rotation);
    }

    [Button]
    public void Test()
    {
        AnimateAttack(false);
    }

}