Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Leadgame · Jul 04, 2015 at 12:25 AM · animationcharactercontrollerpathfindingif-statementsastar

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.

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image incorrect · Jul 04, 2015 at 12:31 AM 0
Share

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.

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges