Animations & More Light

I put animations in again and tweaked the lighting more. Here is a screenshot, at the bottom is a video clip showing the player with no light and personal lighting.



The animation is quite simple. First I'm using Simple Citizens pack, as shown here below, and using the default animation pack that comes with it. You are welcome to get it yourself, its not pricey.


Then I added a NavMesh to the background to define where my character could walk, and added a NavAgent to the character, so they could traverse the area. On top of that, I added this simple script:

using UnityEngine;
using UnityEngine.AI;

public class CharacterAnimator : MonoBehaviour
{
    private Animator _Animator;
    private NavMeshAgent _Agent;
    public string SpeedVariableName = "Speed_f";

    void Start()
    {
        _Animator = GetComponent<Animator>();
        _Agent = GetComponent<NavMeshAgent>();
    }

    void Update()
    {
        var speed = _Agent.velocity.magnitude;
        _Animator.SetFloat(SpeedVariableName, speed);
    }
}

All it does is set the speed variable, so the animation moves that fast.  There are a lot of animations available in this pack, so I could take more advantage of rotational movement and strafing, but its not needed yet. I also needed a script to manage the how the character moves. This is a little larger, but not by much.


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

public class MoveToNavPoint : MonoBehaviour
{
    NavMeshAgent agent;
    readonly static int BackgroundLayer = 1 << 8;
    void Start()
    {
        agent = GetComponent<NavMeshAgent>();
    }

    void Update()
    {
        if (Input.GetMouseButtonDown(0))
        {
            RaycastHit hit;

            if (Physics.Raycast(
                ray: Camera.main.ScreenPointToRay(Input.mousePosition), 
                hitInfo: out hit, 
                maxDistance: 100,
                layerMask: BackgroundLayer))
            {
                agent.destination = hit.point;
                Debug.Log("Hit: " + hit.collider.gameObject.name, hit.collider.gameObject);
                return;
            }

            if (Physics.Raycast(
                ray: Camera.main.ScreenPointToRay(Input.mousePosition),
                hitInfo: out hit,
                maxDistance: 100))
            {
                Debug.Log("Hit (BROKEN): " + hit.collider.gameObject.name, hit.collider.gameObject);
            }
        }
    }

    public void TravelTo(Transform location)
    {
        agent.destination = location.position;
    }

    public void Stop()
    {
        agent.destination = transform.position;
    }
}

Ultimately, on mouse down, if just sees if the item is slected is part of the background layer, and if so, then it finds the exact point and sets the NavMeshAgent's destination. There are also to helper methods for external control (animation triggers) that enable the recording I did below. I.e. recording the video with and without character lighting.

The character lighting allows me to make the player stand out from the background, because different sets of lights apply to it. Unfortunately, I can't use multiple layers of Post Processing Volumes, otherwise the player would have different post processing effects on them as well. I have an idea as to how I can, but its not so important for the amount of work it would take. 

To get a character on different lights, set the layers for rendered items in the scene. I used Background and Game Focus. I have lights that ONLY work for GameFocus and lights that ONLY work for Background. I could put a light in that affects both, to allow character shadow, and I probably will in a later update.

But below is a video showing the two different modes:


Comments

Popular posts from this blog

C# Parser and Code View

A Momentary Lapse of Passion

How to Setup a Multi-Scene Unity Project for Startup