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 DeadKenny · Aug 01, 2013 at 12:53 AM · c#movementaimecanim

AI Mecanim Movement help!!

How would I replace movement direction with one that is not from Input.

As in how would I convert this code into one that the AI can use. As in how can I tell it that h is movement on the horizontal axis and v is vertical?

 float h = Input.GetAxis("Horizontal");
 float v = Input.GetAxis("Vertical");

This is the what the animator uses in mecanim. Does this have to change too?

 anim.SetFloat("Speed", v);
 anim.SetFloat("Direction", h);


So what I need is how to tell the dam thing that float h and v is movemt on horizontal axis and vertical axis.

Is it float h = transform.position.x? I tried that and it just keeps strafing right.

I hope I was clear enough. Please help. Thanks.

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
0

Answer by Chronos-L · Aug 01, 2013 at 02:02 AM

If you have a target, you can use some simple calculation to figure out the motion of your AI.

 public Transform target;
 
 ...

 //A quick way to figure out how fast you need to move by 
 //checking the distance between your AI and its target 
 float speed = Vector3.Distance( target, transform ) * ...; 
 
 float direction = ...;
 
 anim.SetFloat("Speed", speed);
 anim.SetFloat("Direction", direction);
Comment
Add comment · Show 8 · 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 DeadKenny · Aug 01, 2013 at 02:25 AM 0
Share

Ah ok so I got for speed. Still confused a little.

Vector3.Distance(target.position, transform.position) * The movespeed is governed by the animation so nothing here right?.

The guy now walks forward?

Now I got confused I have no idea about the direction? Sorry I'm kinda dumb.

avatar image DeadKenny · Aug 01, 2013 at 02:25 AM 0
Share

He constantly walks forward as in I have not said walk forward.

avatar image DeadKenny · Aug 01, 2013 at 02:27 AM 1
Share

That can be fixed with if statement though right? As in ?

 if(something){
        anim.setfloat("Speed", speed);
 }
avatar image Loius · Aug 01, 2013 at 02:39 AM 1
Share

A direction is not a single float, unless it is in degrees or radians. What is 'direction'? Convert your (h,v) vector into the same measure 'direction' uses.

avatar image Chronos-L · Aug 01, 2013 at 03:28 AM 0
Share

@Dead$$anonymous$$enny

That can be fixed with if statement though right? As in...

You need to know that you are writing codes for an AI. When Input is received from the player, the player have made decisions on a number of things:

  • Where should I go?

  • Which way should I pick?

  • Is there any enemy?

  • Should I ...

You need to incorporate these complex thought process into your code, in order to get a good AI. You can't just insert one line and it will just work. But, you are in the right direction, just keep on adding more and more conditions and decision making and you should get where you want to.

Show more comments

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

17 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 avatar image

Related Questions

Making a bubble level (not a game but work tool) 1 Answer

Mecanim Making an NPC walk 1 Answer

Nav Mesh Agent break between Rotation changes 1 Answer

Basic AI Movement C# 3 Answers

Nav Agent Moving Only Forwards And Backwards 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