• 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
3
Question by Kristijan · Dec 29, 2010 at 10:13 AM · ai

Artificial intelligence detailed help

Haven't the slightest clue how to make an object move around freely. Please don't simply spam scripts and links as you did in all the other AI questions as I have searched them and none really explain how to do it. Obviously people that posted these answers Thought that the owner already has a clue about AI, although in several posts the poster said he has no clue where to start, so pelase explain to me how to actually Make an object, animated if possible, simply roam the terrain without any instructions and without falling off it.....

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

2 Replies

· Add your reply
  • Sort: 
avatar image
4
Best Answer

Answer by Ricardo · Dec 29, 2010 at 10:26 AM

If you have no clue about AI, then this is the wrong place to try and get one - it's too broad a topic. It seems that what you want is a tutorial, but as Unity provides no built-in steering components, there's little you'll be able to do in a pointy-clicky fashion. You could use UnitySteer to accomplish exactly what you want, as shown on this demo, but without a good understanding of the basics you'd only be fiddling with black box.

I'd recommend you start with aigamedev.com, which contains some free resources on top of the paid ones. The main topics you'll want to read up on are:

  • Autonomous steering
  • Obstacle avoidance
  • Pathfinding (not necessary for wandering aimlessly, but it'll probably be useful later on)

You may also want to check out this navigation framework, as the authors are active on the forums, but I'm not sure if they have tutorials or not.

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

Answer by Novodantis 1 · Dec 29, 2010 at 11:04 AM

First, I would recommend reading Ricardo's links; as mentioned, semi-autonomous generic AI isn't easy. I will nevertheless try to give an example here. Please bear in mind this is going to be basic, and best to get the idea with which to start.

A good way to tackle AI is to implement a script that controls an object in the same way a human does. This is best for situations where AI and humans both control the same kind of object [eg. a fighterplane], but works in other situations too.

To begin, detach the controls from the actions. If you have a character script with boolean or float variables for moving, shooting etc [ eg. isShooting (bool) or movingForward (float) ], you can use a second script to control those values either by human or computer commands, while the object script itself is what actually does the moving or shooting [ transform.Translate(0, 0, movingForward); ].

Take a car game, where the car script handles moving the car by carSpeed and turning by steerBy. Those variables should be private to the car script, so we make functions to access them, like:

function Steer(amount : float) {
  steerBy = amount;               // change steering value by whatever is passed
}

Then our human controls object simply finds the object's car script (carScript) and calls the Steer function:

carScript.Steer(Input.Axis("Horizontal"));   // uses the human input to steer

To control this same object with AI, we replace the key controls secondary script with an AI secondary script, which can interface like so:

carScript.Steer(turnRate);  // turn by the AI's variable for steering

This uses the AI script's own variable, called turnRate here, to control the car. All we need now is to change this number and we can steer the car, without fiddling with the car script. For random roaming, simply plug in a sin wave [turnRate = Mathf.Sin(Time.time);]. You can do anything to that number; so long as you get something between +1 and -1, the computer 'plays' by the same rules as the human.

How do you actually detect obstacles or the playfield edge, and transfer this to changing a steering value? Well, I understand this might be the crux of your question but it's also where that 'vastness' really comes in. This is a complex problem, very much determined by your particular game. You can use raycasts forward to the left and right slightly, then turn away from whichever is closest. You could steer toward an object by finding the difference between the "lookAt" angle and the current direction, then steering by it.

Hopefully those basic steps will help you to apply higher-level AI techniques to Unity!

Comment
Add comment · Show 3 · 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 Pe-ads · Dec 29, 2010 at 11:26 AM 0
Share

Thanks a lot!

This really shows how splitting scripts up can save time and CPU power...

Pe-ads

avatar image Novodantis 1 · Dec 29, 2010 at 12:00 PM 0
Share

The main benefit of splitting your script into smaller, interchangable modules is flexibility and reusability. Taking this approach, you don't need to rewrite your AI for three types of car; you just use the same control systems plugged into different objects. You can also change human objects to AI and vice versa with ease.

avatar image Kristijan · Dec 29, 2010 at 02:11 PM 0
Share

Thx for the help guys

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

1 Person is following this question.

avatar image

Related Questions

Ai Zombie Melee Attack script. 5 Answers

Enemy following Player on uneven surface 1 Answer

fog of war/detecting exposed entities 1 Answer

Raycast problems in my AI 0 Answers

Smart Suicide Bomber AI 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