velocity
Velocity at which the obstacle moves around the NavMesh.
Read time 1 minuteLast updated 13 days ago
Definition
- Type: Property
- Namespace: UnityEngine.AI
- Assembly: UnityEngine.AIModule
public Vector3 velocity { get; set; }
Examples
// Name this file (ManualObstacleVelocityUpdater.cs) to match the class name.using System;using UnityEngine;using UnityEngine.AI;/// <summary>/// Update the current GameObject's NavMesh Obstacle velocity according to its position changes./// Useful when the position of an object is controlled by script./// </summary>public class ManualObstacleVelocityUpdater : MonoBehaviour{ NavMeshObstacle m_Obstacle; Vector3 m_LastPosition; void Start() { m_Obstacle = GetComponent<NavMeshObstacle>(); m_LastPosition = transform.position; } void Update() { var deltaTime = Time.deltaTime; if (m_Obstacle && deltaTime > Mathf.Epsilon) { // Compute this frame's velocity var newPosition = transform.position; var velocity = (newPosition - m_LastPosition) / deltaTime; m_Obstacle.velocity = velocity; // Keep track of the last considered position m_LastPosition = newPosition; } }}