- Home /
How do I modify this script to use my character's animations while moving its character controller? Among other things.
I'm trying to make a A* node based movement environment, where the character walks in straight lines from point to point around the house upon clicking on a node that he is not standing on. The pathfinding system is the Simply A* free asset. There seem to be some issues.
For reference and clarification if my terminology is off, maybe this image of the scene will help: http://i.imgur.com/4ImnKNj.png?1
Here is the script first, though:
using UnityEngine;
using System.Collections;
public class PlayerAI : Pathfinding {
public Vector3 moveDirection = Vector3.zero;
public GameObject playerTarget;
void Update () {
FindPath (transform.position, playerTarget.transform.position);
/*
if (Input.GetMouseButtonDown(0)){
FindPath (transform.position, playerTarget.transform.position);
}
*/
if (Path.Count > 1)
{
CharacterController controller = GetComponent<CharacterController>();
moveDirection = new Vector3(transform.position.x-Path[1].x, 0, transform.position.z-Path[1].z);
moveDirection = transform.TransformDirection(moveDirection);
controller.Move(moveDirection*Time.deltaTime*2);
}
else if (Path.Count == 1)
{
CharacterController controller = GetComponent<CharacterController>();
moveDirection = new Vector3(transform.position.x-Path[0].x, 0, transform.position.z-Path[0].z);
moveDirection = transform.TransformDirection(moveDirection);
controller.Move(moveDirection*Time.deltaTime*2);
}
}
}
The first issue is that in all events, I am always receiving an argument out of range error while running the game. I presume this is because of the Path array having only one element in it at times, when I am headed to the last node in a sequence of waypoints. So then I added an else if statement for when there was only one element in the Path array (there are never zero elements in this path array, the "currently occupied" or least distant or last node in a sequence is always Element 0). But that doesn't seem to be working at all, and I think it shows. When approaching the last node of a sequence, it always stops at what would be element 0, so it stops at the node before the destination node. This setup also has the issue of having the character walk between unconnected nodes, smoothing out the walk curve (something I don't want).
If I do uncomment the FindPath function in the OnMouseDown if statement (and remove the FindPath function above it), the character will move to his destination, but he will do so with increasing slowness as he approaches the next node, and he will only move in 1 node per click (which I actually prefer). Furthermore, his movement gets super messed up if I click anywhere during this while he is moving between nodes, and I understand that is because the FindPath function is running again mid-movement. I recognize that this issue is about the OnMouseDown - I need a better condition than that.
What annoys me the most though, is that the idle animation plays throughout the whole moving process. I want the character to walk cleanly through the nodes, with constant speed in a straight line. How do I modify the script to incorporate walking animations for the character, and how do I modify it so that movement doesn't slow down as it approaches the next node? If it helps, I have noticed that when my character is travelling downstairs in my scene, from the top to the bottom of the stairs specifically, he plays the walking animation for a second.
EDIT: Did some experimenting with the whole idea of downhill, and thought that maybe the split second animations had something to do with hitting the ground. So I applied a downward movement by adding a constant 0.5f to moveDirection.y. Camera is jerky now, and walk animation is played even when standing still, but he does walk. So I'm thinking maybe I can bootleg this by attaching the camera to something stationary in the scene, and applying moveDirection.y = 0.5 when his player.transform hasn't changed for some amount of time. But there must be a better solution than this.
When you ask a question, make it one question, not 'how to fix this all' one. :)
The first issue is that in all events, I am always receiving an argument out of range error while running the game.
Please, quote the error as it is shows in Unity console, so we can see in which line of code there is an error.
You can learn how to make and control animations in Unity Animation Lessons.
Your answer
Follow this Question
Related Questions
A* Pathfinding project with mecanim animation 0 Answers
Animation works, pathfinding works, but they don't work together. 1 Answer
Stop animation when character controller hits walls 1 Answer
Not sure why animation plays automatically 1 Answer
How to make AICharacterController able to walk on walls and ceilings? 0 Answers