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 LukaKotar · Oct 04, 2013 at 07:29 PM · c#positionmecanimmove

Mecanim walk to target position

Hi,

I am trying to make my Mecanim character to walk to a position in world space, in some event. How would I make the character walk to that position, and follow it even if the position was changed?

Thanks in advance!

Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by infinitypbr · Oct 05, 2013 at 12:36 AM

Since you mention Mecanim, I'm guessing you want the animation 'walk' to play while the character moves?

First you'll need to make the character move automatically. For that, define a variable Vector3 with the position you'd like the character to walk to. (You could use a NavMeshAgent as well).

To ensure that Mecanim responds with the proper animation, you'll need to get the delta movement of the character, and delta rotation. For that, save the variable Vector3 Position of the character, and compare that with the last one...

Something like:

 var currentPos : Vector3;   // Current position of the player
 var playerAnimator : Animator;   // Assign this in the inspector
 
 function Update () {
   if (currentPos) // won't run on the first frame if currentPos is null)
   {
     var currentVelocity = Vector3.Distance(transform.position, currentPos);    // gets the amount the character moved in the last frame
     currentPos = transform.position;  // reset
     playerAnimator.SetFloat("velocity", currentVelocity);        // Pass currentVelocity to Animator
   }
 }

Do something similar for the rotation. This way the animations, controlled by Velocity and a variable for turning amount (X/Y, basically), can be adjusted when the player isn't pressing controller buttons, and the animations will play properly.

Keep in mind RootMotion won't work with this setup, since the actual movement of the player is being controlled by the NavMesh or through direct code.

Comment
Add comment · Show 2 · Share
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 LukaKotar · Oct 05, 2013 at 01:40 AM 0
Share

Thank you for the effort, however, I can see an error in the code, transform.position returns a Vector3, currentPos is a Vector3, and Vector3 - Vector3 = Vector3. You are passing it to $$anonymous$$echanim as a float.

The Nav$$anonymous$$esh solution could be doable, but I wanted to avoid it, so that I don't need to generate it simply for the character to be moved on occasion. It feels like a waste, unless I end up using it for the AI. A simple solution would be to just turn the player towards the position in world space on the Y axis and trigger the walk animation, but the thing is, I want the character to do more than just walking. The character would walk towards a door, stop and open it, and actually move closer to the door while it is being opened, so that I can guide the I$$anonymous$$ to the door handle accurately. (I have all the positions defined.) If I were to move it through code, I'd have to calculate how fast the player transform is moving, and what speed the animation should be playing at.

I have updated the question's tags with c#. I prefer C# over Unity-JavaScript.

avatar image infinitypbr · Oct 05, 2013 at 02:55 AM 0
Share

You're right -- was supposed to be Vector3.Distnace. I've updated the code. Also added the line required to reset the currentPosition variable for the next frame.

Someone with more knowledge would have to explain how to do the door opening etc, as I've never done anything like that.

If you want the character to simply move forward, then using the LookAt function per frame will help.

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

16 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

C# move y position of object not working 2 Answers

Move block to click 1 Answer

Distribute terrain in zones 3 Answers

Moving GameObject to various position ? 1 Answer


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