首页 文章

如何让角色慢慢开始/停止步行/跑步?

提问于
浏览
-1

我的意思是当角色走路或跑步并到达点击的点时,他将动画师的状态从步行/跑步改为空闲,所以看起来他走路然后停止在步行/跑步和开始之间没有动画/停 .

我在动画师中有3个状态 . HumanoidWalk,HumanoidRun,HumanoidIdle . 我需要一些像褪色的东西 . 例如,如果在行中:

_animator.CrossFade("Walk", 0);

我会将0更改为1,所以当他开始“行走”时,行走速度会慢一些 . 但是在“闲置”中,如果我将其改为1,那么在他停止之前,其他东西不会消失 .

换句话说,我想在角色开始/停止步行/跑步时以及当我点击/双击并在步行和跑步之间切换时添加淡化效果 . 制作一些淡入淡出效果,这样它就不会在状态之间如此快速地切换 .

using UnityEngine;
using System.Collections;

public class ClickToMove : MonoBehaviour
{
    public int speed = 5; // Determines how quickly object moves towards position
    public float rotationSpeed = 5f;

    private Vector3 targetPosition;
    private Animator _animator;
    private Vector3 destination;
    private Quaternion targetRotation;

    public float clickDelta = 0.35f;  // Max between two click to be considered a double click

    private bool click = false;
    private float clickTime;

    void Start()
    {
        _animator = GetComponent<Animator>();
        _animator.CrossFade("Idle", 0);
    }

    void Update()
    {
        if (Input.GetKeyDown(KeyCode.Mouse0))
        {
            if (click && Time.time <= (clickTime + clickDelta))
            {
                _animator.CrossFade("Run", 0);
                click = false;
            }
            else
            {
                click = true;
                clickTime = Time.time;
            }

            _animator.CrossFade("Walk", 0);
            Plane playerPlane = new Plane(Vector3.up, transform.position);
            Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
            float hitdist = 0.0f;

            if (playerPlane.Raycast(ray, out hitdist))
            {
                Vector3 targetPoint = ray.GetPoint(hitdist);
                targetPosition = ray.GetPoint(hitdist);
                targetRotation = Quaternion.LookRotation(targetPoint - transform.position);
                destination = targetPosition;
            }
        }

        transform.rotation = Quaternion.RotateTowards(transform.rotation, targetRotation, rotationSpeed);

        if ((transform.position - destination).magnitude < 0.7f)
        {
            _animator.CrossFade("Idle", 0);
        }
    }
}

1 回答

  • 0

    如果稍微增加变换持续时间值(如0.25),则可以在状态之间实现平滑过渡 . 同时取消选中“固定持续时间” .

相关问题