Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 11 Next capture
2021 2022 2023
1 capture
11 Jun 22 - 11 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 CubePhysics · Jul 13, 2015 at 07:50 PM · aienemyrigidbody2dprojectilevector2

How to detect wether an object is headed towards the top or bottom of an object?

Im working on enemy AI for my game and I have a problem. The enemy has a collider on it and I want it to check for another objects distance (projectile). If the projectile is aimed towards the top of the enemy object, then the enemy should move downwards and if the projectile is aimed at the bottom the enemy should move upwards. I have got a vector2.distance method setup to measure the distance between the enemy and projectile but I want to check if the projectile is aimed at the top or bottom and if the distance of the projectile to the top for example is less than the distance to the bottom then let the enemy AddForce(transform.down * 800 * Time.deltaTime); So its like dodging the projectile.

alt text

untitled.png (6.1 kB)
Comment
Add comment · Show 1
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 maccabbe · Jul 13, 2015 at 07:56 PM 0
Share

Use a raycast. If the ray hits the object at the bottom then move up, otherwise move down.

http://docs.unity3d.com/ScriptReference/Physics.Raycast.html

http://docs.unity3d.com/ScriptReference/RaycastHit-point.html

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by MSplitz-PsychoK · Jul 14, 2015 at 12:08 AM

Dot products will be super useful here! Here's an example of how to use them in this situation, though I highly recommend reading up on them, they are infinitely useful in game programming.

 // Assuming projectile.transform.forward is the direction of the projectile...
 float upperDot = Vector3.Dot(projectile.transform.forward, ((AI.transform.position + AI.transform.up) - projectile.transform.position).normalized);
 float lowerDot = Vector3.Dot(projectile.transform.forward, ((AI.transform.position - AI.transform.up) - projectile.transform.position).normalized);
 
 if (upperDot > lowerDot)
 {
     // Projectile is aimed more towards top
 }
 if (lowerDot > upperDot)
 {
     // Projectile is aimed more towards bottom
 }
 if (lowerDot == upperDot)
 {
     // Projectile is aimed at the center of the AI hitbox
 }
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 nadhimali · Jul 14, 2015 at 02:24 AM

That object has a collider and there is a simple trick and it might work

 ColliderCenterY = GetComponent.<BoxCollider>().bounds.center.y;
 //now you can find where the projectile in respective to object in Y axis 
 if ( ProjectileA.transform.position.y > ColliderCenterY) {

    //move the object down

   }
  if ( ProjectileB.transform.position.y <= ColliderCenterY) {

    //move the object Up

   }



you can also use this code to find the max and min Y points like this

 ColliderTopY = GetComponent.<BoxCollider>().bounds.max.y;
 
 ColliderBottomY = GetComponent.<BoxCollider>().bounds.min.y;
 

and with it you can calculate the distance to top and bottom Y points. As long as you know the Vectors for your objects then you can get what you want.

for more info read this

http://docs.unity3d.com/ScriptReference/Bounds.html

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 MSplitz-PsychoK · Jul 14, 2015 at 05:27 PM 0
Share

Your first solution doesn't work because your projectile can be below the AI, but heading towards his head (imagine in an FPS, you aim at someone's head while they're standing on a ledge above you)

Your second solution will work for most games, but is dependent the AI remaining upright. As soon as you rotate the AI on the x or z axis, this will no longer work.

avatar image nadhimali · Jul 14, 2015 at 05:34 PM 0
Share

yap your right, my answer is good for 2D because i have never worked with 3D before

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

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

Related Questions

Vector2.MoveTowards and Rigidbody2D Velocity 3 Answers

Enemy AI Not Working? 1 Answer

How do I apply bullet projectile to enemy ai properly on unity3d 0 Answers

Need help trying to make enemy detect player's projectiles 1 Answer

Send an enemy back to its spawn point using waypoints 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