Iris Oostra
  • Home
  • Portfolio
    • Math Dungeon
    • VR Portfolio
      • Blendshapes
      • Concepting & Design
      • Group Coaching
      • Individual Coaching
      • Pressure Cooker
      • Programming
    • Pressure Cooker: Candy Sorter
    • Lava Shader
    • Bunny Platformer
    • Hero’s Crucible
    • DnD app
    • Zwangerschapsapp
  • Hobby’s
    • Naaiprojecten
    • Tekeningen
      • Alle tekeningen
      • Digitale tekeningen
      • Marker tekeningen
      • Potloodtekeningen
      • Verf
  • Contact
20 januari 2022

Navmesh, making characters walk

Navmesh, making characters walk
20 januari 2022

This is a new thing I learned from my teammate Scott and something he specifically learned for this project. He teached me about navmesh baking and I made a proximity code.

Scott made a system with waypoints and I used this to make the characters walk on the runway. We couldn’t get the baking to only bake the runway, because it was too narrow, so I used a lot of waypoints to make sure the characters stayed on the runway when walking. We also noticed that once a character arrived at a waypoint they would stand still for a short period of time and this made it so they didn’t walk smooth. I fixed this with code that changes the waypoint to a new waypoint in the list when the character is in close proximity. I got this idea from here, but I made the code myself.

Reflection

What I learned from this is already described in the text above and I know now how I can make my characters walk in future projects and I learned a bit about baking.

Here you can see the code Scott made with my adjustments for the proximity in it:

using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;
using System.Linq;

public class NavMeshTest : MonoBehaviour {
    [SerializeField]
    private List<Transform> waypoints;

    private NavMeshAgent agent;
    private int nextPosition;

    [SerializeField]
    private float proximity = 0.2f;

    void Start() {
        agent = GetComponent<NavMeshAgent>();

        if(agent == null) {
            Debug.LogError("no agent found");
        }

        if(!waypoints.Any() || waypoints[nextPosition] == null) {
            Debug.LogError("no waypoints to go");
        } else {
            Debug.Log("we have " + waypoints.Count + " waypoints");
        }
    }

    void Update() {
        agent.destination = waypoints[nextPosition].position;

        if(/*transform.position != waypoints[nextPosition].position*/CalculateDistance() > proximity) {
            return;
        }

        nextPosition++;


        if(nextPosition != waypoints.Count) {
            return;
        }

        nextPosition = 0;
    }

    private float CalculateDistance() {
        float distanceToNextWaypoint = Vector3.Distance(agent.destination, transform.position);

        return distanceToNextWaypoint;
    }
}

Zoeken

Categorieën

  • Blendshapes
  • Concepting & Design
  • Group Coaching
  • Individual Coaching
  • Pressure Cooker
  • Programming

Meest recente berichten

Concepting & Design23 januari 2022
Introduction programming23 januari 2022
Individual coaching23 januari 2022
Group coaching23 januari 2022
Blend shapes23 januari 2022

Iris Oostra

LinkedIn – GitHub – GitHub(Oud) – DeviantArt

© 2024