Untitled

 avatar
unknown
csharp
3 years ago
1.7 kB
7
Indexable
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;

public class Navmesh_Breaks : MonoBehaviour
{
    public float lerpMove;

    private Quaternion previusRotation;
    private Quaternion currentRotation;
    public float lerpTurn;

    public float currentTurnSpeed;

    [SerializeField]
    Animator animator;
    NavMeshAgent agent;

    private float agentSpeed;
    private float agentTurnSpeed;
    void Start()
    {
        previusRotation = transform.rotation;

        agent = GetComponent<NavMeshAgent>();
        agentSpeed = agent.speed;
        agentTurnSpeed = agent.angularSpeed;
    }

    // Update is called once per frame
    void Update()
    {
        AdjustSpeed();
        Animate();
    }

    void AdjustSpeed()
    {
        if (angleToVelocity > 89 && agent.speed == agentSpeed)
        {
            agent.speed = 0;
        }

        else if (angleToVelocity < 90 && agent.speed != agentSpeed)
        {
            agent.speed = agentSpeed;
        }
    }

    void Animate()
    {
        lerpMove = agent.velocity.magnitude / agentSpeed;


        currentRotation = transform.rotation * Quaternion.Inverse(previusRotation);
        previusRotation = transform.rotation;

        lerpTurn = Quaternion.Angle(transform.rotation, currentRotation);
    }

    float agentVelocity
    {
        get
        {
            return agent.velocity.magnitude;
        }
    }

    float angleToVelocity
    {
        get
        {
            return Vector3.Angle(transform.forward, agent.desiredVelocity);
        }
    }
}
Editor is loading...