在接触门之前,如何将角色的行走速度缓慢降低到0?

时间:2019-01-27 01:14:31

标签: c# unity3d

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Collision : MonoBehaviour
{
    public GameObject door;
    public Animator character;
    public DoorsLockManager doorslockmanager;

    private float speed;

    private void OnTriggerEnter(Collider other)
    {
        if(other.name == door.name &&
           doorslockmanager.locked == true)
        {
            character.SetFloat("Walking Speed", speed);
        }
    }

    // Start is called before the first frame update
    void Start()
    {

    }

    // Update is called once per frame
    void Update()
    {
        float distanceFromTarget = Vector3.Distance(character.transform.position, door.transform.position);

        if (distanceFromTarget < 3)
        {
            speed = (distanceFromTarget / 10) / 1;
        }
    }
}

在这种情况下,我正在检查距门的距离3。 角色确实会缓慢降低步行速度。 但这永远不会阻止角色继续缓慢地穿过门。

例如,如果角色在距门3的距离处开始减速,然后在距离1或0.5f处停止步行速度0,我希望该字符

停止在门前走一点。而不仅仅是突然停止行走,而是逐渐将速度降低到0。

这是一个有效的脚本。但是我仍然对速度计算部分有些困惑:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Collision : MonoBehaviour
{
    public GameObject door;
    public Animator character;
    public DoorsLockManager doorslockmanager;

    private float speed = 0;
    private bool triggered = false;

    private void OnTriggerEnter(Collider other)
    {
        if (other.name == door.name &&
           doorslockmanager.locked == true)
        {
            triggered = true;
        }

        if(doorslockmanager.locked == false)
        {
            triggered = false;
        }
    }

    // Update is called once per frame
    void Update()
    {
        float distanceFromTarget = Vector3.Distance(character.transform.position, door.transform.position);

        if (triggered == true)
        {
            speed = (distanceFromTarget / 10);
            character.SetFloat("Walking Speed", speed);
            character.SetBool("Idle", true);
        }
    }
}

此行:

speed = (distanceFromTarget / 10);

似乎角色在第一次减速时速度过快,而不是缓慢平稳地行走。

1 个答案:

答案 0 :(得分:1)

您可以尝试将maxDistance(3)和minDistance(1)之间的距离映射到10({{3} }):

public float minDistance;
public float maxDistance;
private float initialSpeed;

private void Update()
{
    float distanceFromTarget = Vector3.Distance(character.transform.position, door.transform.position);

    ClampSpeed(distanceFromTarget);
}


private void DampSpeed(float distance)
{
    // value between minDistance and maxDistance 
    // 1-3 in your case
    var clampedDistance = Mathf.Clamp(distance, minDistance, maxDistance);

    // This gives you a value between 0-1
    // where 1 means distance >= maxDistance
    // 0 means distance <= minDistance
    var mapped = (x - minDistance) / (maxDistance - minDistance);

    speed = initialSpeed * mapped;
    character.SetFloat("Walking Speed", speed);
}

您仍然必须以某种方式获得初始速度,并且在启用和禁用此代码块时要格外小心,因为一旦关上门便无法离开门;)

看起来像

// Flag for enabling and disabling damping
private bool enableDamping;

// speed with which the player passed the 3m mark
private float initialSpeed;

private void Update()
{
    float distanceFromTarget = Vector3.Distance(character.transform.position, door.transform.position);

    if(distanceFromTarget > maxDistance)
    {
        // while far away only remember current speed and do nothing else
        initialSpeed = speed;
        enableDamping = true;
    }
    else if (distanceFromTarget <= maxDistance && distanceFromTarget > minDistance)
    {
        if(enableDamping)
        {
            DampSpeed(distanceFromTarget);
        }
    }
    else
    {
        if(enableDamping)
        {  
            // now speed should be zero but just to be sure
            speed = 0;
            character.SetFloat("Walking Speed", speed);
            // and the player minDistance from the door
            // you might want to disable the damping now so you can still move away again.
            enableDaming = false;
        }
    }
}

请注意,它仍然可能会遇到奇怪的情况,因为如果您以非常低的速度进入3m范围,则可能需要一段时间才能最终到达1m并能够再次移动...

但我希望它能给您一个想法