主要问题是,它永远不会进入10或5的任何距离条件。玩家只是不断走动。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class AnimatorController : MonoBehaviour
{
public Animator anim;
public float movingSpeed = 1f;
public bool slowDown = false;
public float rotationSpeed;
private bool startWaitingAnim = true;
private bool endRot = false;
private Vector3 originalPos;
// Start is called before the first frame update
void Start()
{
originalPos = anim.transform.position;
anim.SetFloat("Walking Speed", movingSpeed);
}
// Update is called once per frame
void Update()
{
float distanceFromTarget = Vector3.Distance(originalPos, new Vector3(anim.transform.position.x, anim.transform.position.y, anim.transform.position.z + 10));
if (slowDown)
{
if (distanceFromTarget < 10)
{
float speed = (distanceFromTarget / 10);
anim.SetFloat("Walking Speed", speed);
}
}
if (distanceFromTarget < 5f)
{
anim.SetBool("Idle", true);
if (startWaitingAnim == true)
{
StartCoroutine(WaitForAnimation());
startWaitingAnim = false;
}
anim.SetBool("Rifle Aiming Idle", true);
anim.SetBool("Rifle Aiming Idle", true);
if (!endRot)
{
Quaternion goalRotation = Quaternion.Euler(0f, 0f, 0f);
float angleToGoal = Quaternion.Angle(goalRotation, anim.transform.localRotation);
float angleThisFrame = Mathf.Min(angleToGoal, rotationSpeed * Time.deltaTime);
// use axis of Vector3.down to keep angles positive for ease of use
anim.transform.Rotate(Vector3.up, angleThisFrame);
// We end if we rotated the remaining amount.
endRot = (angleThisFrame == angleToGoal);
}
}
}
bool waitinganimation = false;
IEnumerator WaitForAnimation()
{
yield return new WaitForSeconds(3);
waitinganimation = true;
}
}
我试图在条件上设置一个断点:
if (distanceFromTarget < 10)
但是它永远都不会停在那里,永远不会到达那里,也永远不会在其中执行代码。 如果距离小于5,则不要进入第二个条件。
检查员的脚本截图:
动画师的屏幕截图: