c# - 如果玩家面向另一个方向,如何将“前进”动画状态浮点值缓慢增加到 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 的距离还是现在面对另一个方向?
我现在尝试的是在 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);
}
}
}
解决方案
也许不干净和最好的方法,但这就像我想要的那样工作。
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;
}
}
}
推荐阅读
- php - PHP Logger VPN / Proxy 通过 file_get_contents 和 preg_match 检测
- html - 如何使输入字段仅与可用空间一样宽?
- html - react js内联样式中是否有标签样式
- javascript - Bootstrap 4自定义文件上传未在标签中显示文件名
- r - 编织 Rmarkdown 文件时出错 - 安装 tidyverse 后发生
- php - Laravel 流明没有正确更新
- azure - Azure 工件 - 下载通用工件的特定版本
- sql - 更新在数据库中存储为字符串的 XML 节点
- python-3.x - 如何读取文本数据并转换为 pandas 数据框
- java - 当作者说 - “两个线程试图同时执行一段代码”时,这是什么意思?