Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

How can I stop the character walking speed slowly to 0 before getting to touch the door ?

Discussion in 'Scripting' started by Chocolade, Jan 27, 2019.

  1. Chocolade

    Chocolade

    Joined:
    Jun 19, 2013
    Posts:
    916
    Code (csharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5.  
    6. public class Collision : MonoBehaviour
    7. {
    8.    public GameObject door;
    9.    public Animator character;
    10.    public DoorsLockManager doorslockmanager;
    11.  
    12.    private float speed;
    13.  
    14.    private void OnTriggerEnter(Collider other)
    15.    {
    16.        if(other.name == door.name &&
    17.           doorslockmanager.locked == true)
    18.        {
    19.            character.SetFloat("Walking Speed", speed);
    20.        }
    21.    }
    22.  
    23.    // Start is called before the first frame update
    24.    void Start()
    25.    {
    26.  
    27.    }
    28.  
    29.    // Update is called once per frame
    30.    void Update()
    31.    {
    32.        float distanceFromTarget = Vector3.Distance(character.transform.position, door.transform.position);
    33.  
    34.        if (distanceFromTarget < 3)
    35.        {
    36.            speed = (distanceFromTarget / 10) / 1;
    37.        }
    38.    }
    39. }
    40.  
    In this case I'm checking for distance 3 from the door. The character does slowly reduce the walking speed. But it's never stop the character and keep walking slowly through the door.

    I want the character for example if it start slow down at distance 3 from door then stop walking at speed 0 at distance 1 or 0.5f from the door.

    Stop walking just a bit before the door. And not just suddenly to stop walking but to slowly reduce the speed to 0.
     
  2. AxPetre

    AxPetre

    Joined:
    Jun 1, 2013
    Posts:
    96
    Code (CSharp):
    1. float lerp = Mathf.InverseLerp(1, 3, distanceFromTarget);
    2. speed = NormalSpeed * lerp;
    Place the code above in your update function, instead of that 'if' block. 'NormalSpeed' is a constant value that defines the regular speed with which your characters is moving.
     
  3. Pavlon

    Pavlon

    Joined:
    Apr 15, 2015
    Posts:
    191
    I have some thing similar lying around maybe you can get some use out of it.

    Code (CSharp):
    1. public class CharacterMovement : MonoBehaviour
    2. {
    3.     private Transform targetTransform;
    4.     private float rotationSpeed = 2.0f;
    5.     private float runnSpeed = 1.0f;
    6.     private float walkSpeed = 0.5f;
    7.     private float currentSpeed = 0.5f;
    8.     private float engageDistance = 1.0f;
    9.     private float slowDownDistance = 4.0f;
    10.     private bool isMoving = false;
    11.     private bool isWalking = false;
    12.     private bool isRunning = false;
    13.     private bool variableSpeed = true;
    14.     private bool reachedTarget = false;
    15.      private Animator animator;
    16.     private float animationFadeTime = 1.5f;
    17.  
    18.     // Use this for initialization
    19.     void Start ()
    20.     {
    21.         animator = this.transform.GetComponent<Animator>();
    22.     }
    23.    
    24.     // Update is called once per frame
    25.     void Update ()
    26.     {
    27.         if(targetTransform == null)
    28.             return;
    29.        
    30.         RotateTowardsTarget();
    31.  
    32. //        if(distance > engageDistance && !isMoving) targets cant move away...
    33. //            StartToMove();
    34.  
    35.         if(reachedTarget || !isMoving)
    36.             return;
    37.        
    38.         this.transform.position += (new Vector3(this.transform.forward.x,0,this.transform.forward.z) * currentSpeed) * Time.deltaTime;
    39.  
    40.         float distance = Vector3.Distance(this.transform.position,targetTransform.position);
    41.  
    42.         if(variableSpeed)
    43.         {
    44.             if(isWalking && distance > slowDownDistance)
    45.             {
    46.                 IEnumerator coroutine;
    47.                 coroutine = ChangeSpeed(animationFadeTime,runnSpeed,"Walk","Runn");
    48.                 StartCoroutine(coroutine);
    49.                 isWalking = false;
    50.                 isRunning = true;
    51.             }
    52.             if(isRunning && distance < slowDownDistance)
    53.             {
    54.                 IEnumerator coroutine;
    55.                 coroutine = ChangeSpeed(animationFadeTime,walkSpeed,"Runn","Walk");
    56.                 StartCoroutine(coroutine);
    57.                 isRunning = false;
    58.                 isWalking = true;
    59.             }
    60.         }
    61.         if(distance < engageDistance)
    62.         {
    63.             animator.SetBool("Walk",false);
    64.             animator.SetBool("Runn",false);
    65.             isMoving = false;
    66.             isRunning = false;
    67.             isWalking = false;
    68.             reachedTarget = true;
    69.         }
    70.     }
    71.     public void StartToMove(Transform target,string animationType)
    72.     {
    73.         targetTransform = target;
    74.         isMoving = true;
    75.         isWalking = true;
    76.         currentSpeed = walkSpeed;
    77.         animator.SetBool(animationType,true);
    78.     }
    79.     public void StartToMove(Transform target)
    80.     {
    81.         targetTransform = target;
    82.         isMoving = true;
    83.         isWalking = true;
    84.         currentSpeed = walkSpeed;
    85.         animator.SetBool("Walk",true);
    86.     }
    87.     public void StartToMove()
    88.     {
    89.         isMoving = true;
    90.         isWalking = true;
    91.         currentSpeed = walkSpeed;
    92.         animator.SetBool("Walk",true);
    93.     }
    94.     private void RotateTowardsTarget()
    95.     {
    96.         Vector3 direction = (targetTransform.position - this.transform.position).normalized;
    97.         Quaternion lookRotation = Quaternion.LookRotation(new Vector3(direction.x, 0.0001f, direction.z));
    98.         transform.rotation = Quaternion.Slerp(transform.rotation, lookRotation, Time.deltaTime * rotationSpeed);
    99.     }
    100.  
    101.     IEnumerator ChangeSpeed(float duration,float targetSpeed,string currentAnimation,string exitAnimation)
    102.     {
    103.         float elapsedTime = 0.0f;
    104.  
    105.         while(elapsedTime < duration)
    106.         {
    107.             if(reachedTarget)
    108.                 break;
    109.             if(duration > duration / 2.0f)
    110.             {
    111.                 animator.SetBool(currentAnimation,false);
    112.                 animator.SetBool(exitAnimation,true);
    113.             }
    114.             currentSpeed = Mathf.Lerp(currentSpeed,targetSpeed,elapsedTime / duration);
    115.             elapsedTime += Time.deltaTime;
    116.             yield return null;
    117.         }
    118.     }
    119.  
    120.     public bool ReachedTarget{get{return reachedTarget;}set{reachedTarget = value;}}
    121.     public bool IsMoving{get{return isMoving;}set{isMoving = value;}}
    122.     public float CurrentSpeed{get{return currentSpeed;}set{currentSpeed = value;}}
    123. }
    124.