首页 > 解决方案 > 如果玩家面向另一个方向,如何将“前进”动画状态浮点值缓慢增加到 1?

问题描述

在播放器动画控制器而不是我的控制器中,有一个参数名称 Forward。当我按下 W 键向前移动玩家时,它会将“向前”的值从 0 增加到 1 min 0 max 1。从空闲状态向前为 0 到向前行走状态为 1。

在附加到播放器的脚本中,当播放器到达一定距离时,播放器会慢慢平滑地从步行变为空闲直到停止。

问题是现在“前进”值一直为 0。我想让玩家只有在向前移动并到达距离然后慢慢停止时才允许向后移动并行走,但是如果我按下 S 向后移动然后增加慢慢地将值恢复为 1。

using System.Collections;
using System.Collections.Generic;
using TMPro;
using UnityEngine;
using UnityStandardAssets.Characters.ThirdPerson;

public class DistanceCheck : MonoBehaviour
{
    public GameObject distanceTarget;
    public GameObject descriptionTextImage;
    public TextMeshProUGUI text;

    private Animator anim;

    float timeElapsed;
    float lerpDuration = 3;

    float startValue = 1;
    float endValue = 0;
    float valueToLerp;

    // Start is called before the first frame update
    void Start()
    {
        anim = transform.GetComponent<Animator>();
    }

    // Update is called once per frame
    void Update()
    {
        var distance = Vector3.Distance(transform.position, distanceTarget.transform.position);

        if (distance >= 61f)
        {
            if (timeElapsed < lerpDuration)
            {
                valueToLerp = Mathf.Lerp(startValue, endValue, timeElapsed / lerpDuration);
                anim.SetFloat("Forward", valueToLerp);
                timeElapsed += Time.deltaTime;
            }
            anim.SetFloat("Forward", valueToLerp);
            descriptionTextImage.SetActive(true);
            text.text = "I can't move that far by foot. I need to find some transportation to move any further.";
        }
        else
        {
            text.text = "";
            descriptionTextImage.SetActive(false);
        }
    }
}

这部分让玩家从行走到闲置慢慢停止:

if (timeElapsed < lerpDuration)
                {
                    valueToLerp = Mathf.Lerp(startValue, endValue, timeElapsed / lerpDuration);
                    anim.SetFloat("Forward", valueToLerp);
                    timeElapsed += Time.deltaTime;
                }
                anim.SetFloat("Forward", valueToLerp);

这是一个屏幕截图,显示玩家已到达距离并停止。我将相机移到一边:

播放器

现在我按下键 A 或者因为我用鼠标改变了相机视图所以我按下了 S 但没关系的关键是当我将玩家转向另一个方向然后面向距离 61 玩家应该通过将“前进”值增加到 1 来再次开始缓慢行走。

但是我怎么知道玩家面对的地方是我的意思是他仍然面对 61 的距离还是现在面对另一个方向?

玩家面向另一个方向,而不是距离 61 相反的方向,但我不能让他走,因为我不知道在脚本中如何判断玩家现在面向相反的方向。

我现在尝试的是在 else 部分使用一个新的辅助变量名称 newDir 来检查玩家面对的方向,然后增加“Forward”值,但它什么也没做:

using System.Collections;
using System.Collections.Generic;
using TMPro;
using UnityEngine;
using UnityStandardAssets.Characters.ThirdPerson;

public class DistanceCheck : MonoBehaviour
{
    public GameObject distanceTarget;
    public GameObject descriptionTextImage;
    public TextMeshProUGUI text;

    private Animator anim;

    float timeElapsed;
    float lerpDuration = 3;

    float startValue = 1;
    float endValue = 0;
    float valueToLerp;

    private Vector3 currentDir;
    private Vector3 newDir;

    // Start is called before the first frame update
    void Start()
    {
        anim = transform.GetComponent<Animator>();
    }

    // Update is called once per frame
    void Update()
    {
        var distance = Vector3.Distance(transform.position, distanceTarget.transform.position);

        if (distance >= 61f)
        {
            if (timeElapsed < lerpDuration)
            {
                valueToLerp = Mathf.Lerp(startValue, endValue, timeElapsed / lerpDuration);
                anim.SetFloat("Forward", valueToLerp);
                timeElapsed += Time.deltaTime;
            }
            anim.SetFloat("Forward", valueToLerp);
            currentDir = transform.forward;
            descriptionTextImage.SetActive(true);
            text.text = "I can't move that far by foot. I need to find some transportation to move any further.";
        }
        else
        {
            if(newDir != currentDir)
            {
                startValue = 0;
                endValue = 1;
                if (timeElapsed < lerpDuration)
                {
                    valueToLerp = Mathf.Lerp(startValue, endValue, timeElapsed / lerpDuration);
                    anim.SetFloat("Forward", valueToLerp);
                    timeElapsed += Time.deltaTime;
                }
                anim.SetFloat("Forward", valueToLerp);
            }
            text.text = "";
            descriptionTextImage.SetActive(false);
        }
    }
}

标签: c#unity3d

解决方案


也许不干净和最好的方法,但这就像我想要的那样工作。

using System.Collections;
using System.Collections.Generic;
using TMPro;
using UnityEngine;
using UnityStandardAssets.Characters.ThirdPerson;

public class DistanceCheck : MonoBehaviour
{
    public GameObject distanceTarget;
    public GameObject descriptionTextImage;
    public TextMeshProUGUI text;

    private Animator anim;

    float timeElapsed = 0;
    float lerpDuration = 3;
    float startValue = 1;
    float endValue = 0;
    float valueToLerp = 0;

    // Opposite Direction
    float timeElapsedOpposite = 0;
    float lerpDurationOpposite = 3;
    float startValueForOpposite = 0;
    float endValueForOpposite = 1;
    float valueToLerpOpposite = 0;   

    float angle;

    // Start is called before the first frame update
    void Start()
    {
        angle = transform.eulerAngles.y;
        anim = transform.GetComponent<Animator>();
    }

    // Update is called once per frame
    void Update()
    {
        var distance = Vector3.Distance(transform.position, distanceTarget.transform.position);
        angle = transform.eulerAngles.y;
        if (distance >= 61f && angle < 180)
        {
            if (timeElapsed < lerpDuration)
            {
                valueToLerp = Mathf.Lerp(startValue, endValue, timeElapsed / lerpDuration);
                anim.SetFloat("Forward", valueToLerp);
                timeElapsed += Time.deltaTime;
            }
            anim.SetFloat("Forward", valueToLerp);
            timeElapsedOpposite = 0;
            descriptionTextImage.SetActive(true);
            text.text = "I can't move that far by foot. I need to find some transportation to move any further.";
        }
        else
        {
            text.text = "";
            descriptionTextImage.SetActive(false);
        }

        // Get the angle:
        

        if (angle > 180f && distance >= 61f)
        {
            if (timeElapsedOpposite < lerpDurationOpposite)
            {
                valueToLerpOpposite = Mathf.Lerp(startValueForOpposite, endValueForOpposite, timeElapsedOpposite / lerpDurationOpposite);
                anim.SetFloat("Forward", valueToLerpOpposite);
                timeElapsedOpposite += Time.deltaTime;
            }
            anim.SetFloat("Forward", valueToLerpOpposite);

            timeElapsed = 0;
        }
    }
}

推荐阅读