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 oz m · Jul 25, 2010 at 02:11 AM · aimonster

AI Monster Problem

I already asked a question like this, and got one answer that didn't help much. I fixed my problem to a degree but I still had quite a few problems. I made a monster that walks away from the player when they get close. My problem is that if the player gets too close, the monster starts moving up in the air. If the player is above the monster, it goes under the terrain. Also, if anyone know how to make the monster roam randomly when the player isn't close, please tell me. Here's my script:

var person : Transform; var detected : boolean = false; function Update () {

if(Vector3.Distance(person.position,transform.position) <= 30) { detected = true; } else { detected = false; }

if(detected) { var relativePos = person.position - transform.position; relativePos = relativePos * -1; var rotation = Quaternion.LookRotation(relativePos);

 transform.rotation = rotation;
 animation.CrossFade("walk");
 transform.Translate(Vector3.forward * (Time.deltaTime * 10));

} else { animation.CrossFade("idle"); }

}

Comment
Add comment · Show 2
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 oz m · Jul 25, 2010 at 02:55 AM 0
Share

maybe it would help if there was a way to clamp the rotation of all axes but the y. Can anyone tell me how to do that?

avatar image oz m · Jul 25, 2010 at 04:29 AM 0
Share

I'm still watching this question, so please answer it if you know the answer.

3 Replies

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

Answer by StephanK · Jul 25, 2010 at 09:45 AM

If you are in 2.5D (moving only on the ground) you can just set relativePosition.y = 0 before calculating the lookRotation. Or you can just use transform.LookAt(relativePosition), which is supposed to ignore the y-component and keep your up vector at (0,1,0).

For making your character roam look up AI wander behaviour. It basically works by creating a random point in front of your character/monster and move towards it every frame.

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 oz m · Jul 25, 2010 at 05:06 PM 0
Share

Both don't really work, the first one doesn't work because I have a terrain with hills in it, the monster goes right through. The second one doesn't work at all. Do I need it to have a character controller or something?

avatar image StephanK · Jul 25, 2010 at 11:01 PM 0
Share

If you are using a characterController or anything that takes care of not falling through terrain and staying on the ground this should work.

avatar image
2

Answer by Magnus Wolffelt · Jul 25, 2010 at 06:15 PM

This is a classic example where a raycast is useful.

After moving the monster away from the player, simply raycast with a downward vector using a proper layer mask, to find the "ground point" below the monster. This is where the monster should be positioned:

var hit : RaycastHit;
if (Physics.Raycast(transform.position - Vector3.up * 100.0f, -Vector3.up, hit, Mathf.Infinity, myLayerMask)) {
    transform.position = hit.point;
}

Note that the raycast is performed from 100 units above the monster, to cover the case where the monster has been pushed into the ground. Otherwise you risk missing the terrain with the raycast.

Also, I suggest you make a public variable for the LayerMask - they are a bit tricky to construct correctly with code. Just create a new layer called "Terrain" and specify everything in the editor. Without a layer mask you risk hitting the monster with the raycast, which will result in amusing bugs.

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
0

Answer by slkjdfv · Jul 25, 2010 at 05:38 PM

If you want to lock a rotation use a configurable joint. thats how i kept my doors from rotating in ways i dont want them to :) configurable joint shouldn't affect your characters animation model

Comment
Add comment · Show 1 · 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 oz m · Jul 25, 2010 at 05:42 PM 0
Share

That doesn't work.

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

No one has followed this question yet.

Related Questions

How to make a giant walking monster 1 Answer

Wandering AI Monster 2 Answers

Enemy AI scripting help 1 Answer

Free Roaming AI 2 Answers

Enemy AI monster problem 3 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