Skip to content
This repository has been archived by the owner on Apr 30, 2024. It is now read-only.

Movement for characters #14

Open
PorkDevMode opened this issue Jul 4, 2023 · 1 comment
Open

Movement for characters #14

PorkDevMode opened this issue Jul 4, 2023 · 1 comment

Comments

@PorkDevMode
Copy link

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;
}

}

@Interesting-exe
Copy link
Owner

you should start sending the entire code in code blocks

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;
}
}

@Interesting-exe Interesting-exe pinned this issue Jul 29, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants