3D in Unity

This is 2D graphics pretending to be 3D graphics:

This is 3D pretending to be 2D:

The code behind this is delightfully simpler than what I was using before. I bake myself a navmesh, drop an AI agent on my player, and just tell it to navigate where I clicked.

using UnityEngine;
using UnityEngine.AI;

public class navOnClick : MonoBehaviour
    void Update()
        if (Input.GetMouseButtonDown(0))
            Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);

            RaycastHit hit;
            if (Physics.Raycast(ray, out hit, Mathf.Infinity))
                var n = GetComponent<NavMeshAgent>();

Obviously, this code is a quick and dirty test. I would cache the camera rather than calling Camera.main. But! But. All I need to do to make a gamepad or keyboard do the same trick is call SetDestination() on a point adjacent to the player. Baddah bing, baddah boom, touch and mouse and DPad all work.

Navmeshes apparently only work with mesh renderers and terrain. Not, as I had expected, with colliders. That is a wrinkle, as I have no intention to build my scenes out of mesh renderers or terrain. However, while the navmesh will not bake with the mesh renderer disabled, once it has baked, you can freely disable the mesh and even delete the renderer (although obviously without the collider, you have nothing to catch the raycast).

The next question is: can I tuck a Unity interface into the character and have it spring out at a click?

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s