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;
}
}
}
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 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 speed 0 at distance 1 or 0.5f
Stop walking just a bit before the door. And not just suddenly to stop walking but to slowly reduce the speed to 0.
This is a working script. But I'm still confuse a bit about the speed calculation part:
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);
}
}
}
This line:
speed = (distanceFromTarget / 10);
It seems like the character is slowing down too fast at the first time instead slowly slow down smooth form walking.