• 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 El ChiNo · Jan 25, 2011 at 07:31 PM · aidistance

How to avoid enemies walk up in the air

Hi everyone, I have a little problem that got stucked in a different question posted earlier t$$anonymous$$s week. I have an enemy that follows the player when he's at a certain distance. The t$$anonymous$$ng is that when the enemy is chasing the player, if he jumps, the Ai start to walk in the Y axis trying to reach the player in the air. How can I make the enemy follow the player, without walking in the air, keeping $$anonymous$$m on the ground, no matter if the player jumps or not?

Thanks!

Here's the script so far:

var LookAtTarget:Transform; var speed = 3.0; var proximity = 3.0; var maxDistance = 1.0;

function Start () { animation.wrapMode = WrapMode.Loop; }

function Update(){ var dist = Vector3.Distance(LookAtTarget.transform.position, transform.position);

     //check whether you are wit$$anonymous$$n target proximity
 if ( dist < proximity && dist > maxDistance) {
     animation.CrossFade("threaten");
     transform.LookAt (LookAtTarget); //you look at player with t$$anonymous$$s
     transform.Translate (0,0,speed*Time.deltaTime);

//moves with speed in local z and you are looking toward $$anonymous$$m so you move toward the player //write other codes here } else if (dist < maxDistance) { animation.CrossFade("attack"); transform.LookAt (LookAtTarget); transform.Translate (0,0,0); } else { animation.CrossFade("idle"); }
}

Comment
Add comment · Show 3
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 Scribe · Jan 25, 2011 at 07:40 PM 0
Share

you might try in the transform.LookAt (LookAtTarget); part of your script put: transform.LookAt(Vector3(LookAtTarget.position.x, 0, LookAtTarget.position.z));

not sure if this will work at all thats why I did it as a comment. hope it helps anyway :)

avatar image El ChiNo · Jan 25, 2011 at 08:55 PM 0
Share

Thanks Scribe, it works, but if the player jumps over the enemy, the Ai goes kinda crazy for that moment, it evens gets rotated 90 and layed down to face the player being over it.

avatar image Bunny83 · Jan 25, 2011 at 09:54 PM 0
Share

use transform.LookAt(Vector3(LookAtTarget.position.x, transform.position.y, LookAtTarget.position.z)); instead. My guess is that the worlds y 0-point is below your floor. When X and Z gets very small the resulting vector points almost straight down. by replacing the "0" with transform.position.y the resulting vector always lay on the X-Z-plane relative to your enemy.

2 Replies

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

Answer by Bunny83 · Jan 25, 2011 at 07:58 PM

Like Scribe mentioned you should eliminate the y-part of the direction. That way your enemy turns just around the y-axis. I don't know what kind of game you try the make 2D sidescroll or 3D but anyway, it would be better to move your enemy with a charactercontroller or rigidbody. As far as i know if you set the position directly via Transform you bypass any collision detection. Add a characterController and use it's Move() function (works similar to Transform.Translate()). That would apply gravity to your enemy and it can walk ramps up and down.

edit

transform.LookAt(Vector3(LookAtTarget.position.x, transform.position.y, LookAtTarget.position.z)); 

instead of

transform.LookAt(Vector3(LookAtTarget.position.x, 0, LookAtTarget.position.z)); 


edit

transform.LookAt(Vector3(LookAtTarget.position.x, transform.position.y, LookAtTarget.position.z));
transform.localEulerAngles = Vector3(0,transform.localEulerAngles.y,0);

The second line just eliminates the rotation around x and z. Now it's impossible for the enemy to rotate around these axes.

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 El ChiNo · Jan 25, 2011 at 08:50 PM 0
Share

Sorry for not being clear with my question, the game is a 3d, the player has a third person controller, and the script posted above is for the enemy.

avatar image El ChiNo · Jan 26, 2011 at 02:28 PM 0
Share

Thanks Scribe and Bunny83. It's working better than before. The Ai doesn't walk in the air anymore!. However, if the player jumps and stays over the enemy (like being stand on his head), the Ai still gets laydown on the ground, facing upward (being the player on top of him). I do not know how to avoid this.

avatar image Bunny83 · Jan 28, 2011 at 09:01 PM 0
Share

Ok, i guess you want to rotate you enemy just around y-axis, right? I will edit the answer to avoid rotation around x ans z

avatar image
1

Answer by rasheedqw · Sep 15, 2013 at 07:01 AM

var target:Transform;

var rotationSpeed:int=2;

function Awake(){ myTransform = transform;

}

var lookPos = target.position - transform.position; lookPos.y = 0; var rotation = Quaternion.LookRotation(lookPos); transform.rotation = Quaternion.Slerp(transform.rotation, rotation, Time.deltaTime * rotationSpeed);

what you want is the character to stop rotating on the y axis the y is up z is back and forth and x is side to side i had the same problem t$$anonymous$$s solved it 100 percent

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

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

Adding a load level to this script. 1 Answer

Distance before shooting issue. 1 Answer

Enemy AI help with height check 1 Answer

how to avoid enemies pushing me when they reach me 1 Answer

How would I confine an object to the ground? 2 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