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 cowmilk9 · May 29, 2014 at 03:27 AM · raycastvector3forceup

How to find an object directly above another w/out Raycast?

I am trying to make an object that applies a little air blast to anything that gets above it. I am a little stuck here. The air blaster (aka Jet) should be able to find out if something is directly above it (relative to the jet's orientation) and, if so, apply a wind force that blasts the other object away. I have everything working except I can't make it work only if the other object is DIRECTLY above it.

I've tired using the dot product between the up Vector and the Vector between the object and the jet; I've tried a distance check; I've tried a combination of the two; I've even tried a Raycast. The Raycast would've worked only it would hit other objects (ones that I DON'T want to blast) and return Null Reference Exceptions.

Here's my code as it looks now:

 public class JetController : MonoBehaviour 
 {
     public int windForce = 0;
     public GameObject[] balls;
     
     // Use this for initialization
     void Start () 
     {
         balls = GameObject.FindGameObjectsWithTag ("Ball");
     }
     
     // Update is called once per frame
     void FixedUpdate () 
     {
         Vector3 wind;
         balls = GameObject.FindGameObjectsWithTag ("Ball");
 
         for (int i = 0; i < balls.Length; i++)
         {
             Vector3 heading = balls[i].transform.position - transform.position;
             if (balls[i].transform.tag == "Ball")
             {
                 if (Vector3.Distance(balls[i].transform.position, transform.position) < 2)
                 {
                     if (Vector3.Dot(heading, -transform.forward) > 0)
                     {
                         print (Vector3.Dot(heading, transform.up));
                         wind = transform.up * windForce;
                         balls[i].rigidbody.AddForce(wind);
                     }
                 }
             }
         }
     }

If anyone could help, that would be much appreciated!

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 Kiwasi · May 29, 2014 at 03:38 AM 0
Share

Use a layer mask on your raycast?

1 Reply

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

Answer by supernat · May 29, 2014 at 03:44 AM

There are a couple of ways to handle it. First off, if you wanted to use ray casts, you can limit the ray cast to specific layers. So if you assigned the balls to a Ball layer, pass the ball layer to the raycast method, and it will only return collisions with balls. If you want to use the dot product, I'm a bit confused by you using the trasform.forward for the angle test (negative of that no less) and transform.up for the force. I suspect your Dot(heading, -transform.forward) is returning 0 when the object is directly above the jet. You probably mean Dot(heading, transform.up) here. Then testing > 0 will result in anywhere from the object being parallel with the jet UP direction all the way to being ALMOST 90 degrees perpendicular to the jet UP direction. Hope that makes sense, but feel free to comment if I'm misinterpreting the code.

Also, you don't need to check if transform.tag == "Ball" on line 21 since you already know this.

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.

Follow this Question

Answers Answers and Comments

22 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

Related Questions

AddForce to the Left? 3 Answers

Unity Rotate Raycast on Quaternion 1 Answer

How to access component that the raycast hit 1 Answer

My Raycast on awake fires and will not change Help! 0 Answers

Multiple Raycasts & Vector3.Reflects in a row. How? 1 Answer


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