You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository has been archived by the owner on Apr 30, 2024. It is now read-only.
Not a issue, it might help some people who want random movement for characters if u want to add it here.
Guide:
Make unity c# script
Paste code
Attach to desired character with a navmesh agent attached to said character
Add navmesh terrain to your ground
Adjust in inspector
Code:
using UnityEngine;
using UnityEngine.AI;
public class AIMovement : MonoBehaviour
{
public float movementInterval = 30f; // Interval between movements
public float movementRadius = 1f; // Radius within which the character will move
private NavMeshAgent agent;
private float timer;
private bool canMove = true;
private bool isCameraFollowing = false;
private void Start()
{
agent = GetComponent<NavMeshAgent>();
timer = movementInterval;
}
private void Update()
{
if (!isCameraFollowing)
{
timer -= Time.deltaTime;
if (canMove && timer <= 0f)
{
MoveRandomly();
timer = movementInterval;
}
}
}
private void MoveRandomly()
{
Vector3 randomDirection = Random.insideUnitSphere * movementRadius;
randomDirection += transform.position;
NavMeshHit hit;
// Find a valid random point on the NavMesh to move to
if (NavMesh.SamplePosition(randomDirection, out hit, movementRadius, NavMesh.AllAreas))
{
agent.SetDestination(hit.position);
}
}
public void SetCameraFollowing(bool following)
{
isCameraFollowing = following;
if (isCameraFollowing)
{
canMove = false;
agent.isStopped = true;
agent.ResetPath();
Invoke("ResumeMovement", 5f);
}
}
private void ResumeMovement()
{
canMove = true;
agent.isStopped = false;
}
}
The text was updated successfully, but these errors were encountered:
Not a issue, it might help some people who want random movement for characters if u want to add it here.
Guide:
Make unity c# script
Paste code
Attach to desired character with a navmesh agent attached to said character
Add navmesh terrain to your ground
Adjust in inspector
Code:
using UnityEngine;
using UnityEngine.AI;
public class AIMovement : MonoBehaviour
{
public float movementInterval = 30f; // Interval between movements
public float movementRadius = 1f; // Radius within which the character will move
private NavMeshAgent agent;
private float timer;
private bool canMove = true;
private bool isCameraFollowing = false;
}
The text was updated successfully, but these errors were encountered: