Moonwalking

Hello!

Course is going well, but I noticed something a while ago and finally decided to ask after being stumped trying to resolve the issue;

I noticed that if I choose a position that is directly behind the unit, they don’t like to turn around (sometimes they do, slowly, but other times they don’t at all). Anyone else run into this issue?

Below is my MoveAction code if anyone has any thoughts:

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;

public class MoveAction : BaseAction
{
[SerializeField] private int maxMoveDistance = 4;
[SerializeField] private Animator unitAnimator;

float moveSpeed = 4f;
float stoppingDistance = .1f;
private Vector3 targetPosition;

float rotateSpeed = 10f;

protected override void Awake()
{
    base.Awake();
    targetPosition = transform.position;
}

private void Update()
{
    if (!isActive){ return;}

    Vector3 moveDirection = (targetPosition - transform.position).normalized;

    transform.forward = Vector3.Lerp(transform.forward, moveDirection, Time.deltaTime * rotateSpeed);

    if (Vector3.Distance(transform.position, targetPosition) > stoppingDistance)
    {
        unitAnimator.SetBool("IsWalking", true);
        transform.position += moveDirection * moveSpeed * Time.deltaTime;
    }
    else
    {
        unitAnimator.SetBool("IsWalking", false);
        ActionComplete();
    }

}

public override void TakeAction(GridPosition gridPosition, Action onActionComplete)
{
    ActionStart(onActionComplete);
    this.targetPosition = LevelGrid.Instance.GetWorldPosition(gridPosition);
}
public override List<GridPosition> GetValidActionGridPositionList()
{
    List<GridPosition> validGridPositionList = new List<GridPosition>();

    GridPosition unitGridPostion = unit.GetGridPosition();

    for(int x = -maxMoveDistance; x <= maxMoveDistance; x++)
    {
        for(int z = -maxMoveDistance; z <= maxMoveDistance; z++)
        {
            //Same Grid Position where the unit is already at
            //Grid position occupied by other Unit

            GridPosition offsetGridPosition = new GridPosition(x, z);
            GridPosition testGridPosition = unitGridPostion + offsetGridPosition;

            if (!LevelGrid.Instance.IsValidGridPosition(testGridPosition))
            { continue; }

            if (unitGridPostion == testGridPosition) {continue;}

            if (LevelGrid.Instance.HasUnitAtGridPosition(testGridPosition)) {continue; }

            validGridPositionList.Add(testGridPosition);
        }
    }

    return validGridPositionList;
}

public override string GetActionName(){ return "Move";}

}

There are several posts asking and answering this same question. Here’s a good one with a good explanation of what’s happening, too

The TL:DR of which is, when dealing with rotations, it is better to Slerp than Lerp.

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms