• 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 /
This question was closed Jul 30, 2014 at 07:35 AM by raycosantana for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by raycosantana · Jul 29, 2014 at 02:21 PM · raycastdirection

Raycast forward except for the Y axis

I need to Raycast forward from the enemy except for the Y axis w$$anonymous$$ch should point to the player

Basically I want the enemy raycast direction to be forward but also point to the players height, because when the player is on for example a ramp the enemy cant see it.

Here is an image is an image to illustrate the problem.

alt text

T$$anonymous$$s is the raycast function.

 void FixedUpdate(){
     sight.origin = new Vector3(transform.position.x, transform.position.y + 0.5f, transform.position.z);
     sight.direction = transform.forward;
     RaycastHit rayHit;
     //Vector3 fwd = transform.TransformDirection(Vector3.forward);
 
     if (Physics.Raycast(sight, out rayHit, chaseRange)){
         Debug.DrawLine(sight.origin, rayHit.point, Color.red);
         if (rayHit.collider.tag == "Player"){
             CurrentState = EnemyStates.following;
         }
 
         if (rayHit.collider.tag != "Player" && rayHit.collider.tag != "Enemy"){
             CurrentState = EnemyStates.idle;
         }
     }
     
 }


problem.jpg (95.1 kB)
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

3 Replies

  • Sort: 
avatar image
0
Best Answer

Answer by robertbu · Jul 29, 2014 at 03:44 PM

T$$anonymous$$s is an ugly little problem. It's made a bit uglier by the fact that 1) the origin of your ray is not at the transform.position of the enemy, and 2) the enemy can also be on a slope (I'm assuming). I keep t$$anonymous$$nking there is an easier way to do t$$anonymous$$s if I just t$$anonymous$$nk of the problem correctly, but here is a brute force solution. Note that 'player' is the transform of the player:

 Vector3 sight.origin = transform.position + 0.5 * transform.up;
 Vector3 localPlayerPos = transform.InverseTransformPoint(player.position);
 Vector3 localEnemyPos = transform.InverseTransformPoint(sight.origin);
 Vector3 localPlayerDir = localPlayerPos - localEnemyPos;
 Vector3 v = localPlayerDir;
 v.y = 0.0;
 localPlayerDir = Quaternion.FromToRotation(v, Vector3.forward) * localPlayerDir;
 Vector3 sight.direction = transform.TransformDirection(localPlayerDir);

Note that in your original code, your calculation of site.origin does not take into account that the enemy can be on a slope.

Comment
Add comment · Show 8 · 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 raycosantana · Jul 29, 2014 at 05:07 PM 0
Share
avatar image robertbu · Jul 29, 2014 at 05:44 PM 1
Share
avatar image robertbu · Jul 29, 2014 at 06:34 PM 1
Share
avatar image robertbu · Jul 29, 2014 at 09:09 PM 1
Share
avatar image robertbu · Jul 29, 2014 at 10:12 PM 1
Share
Show more comments
avatar image
0

Answer by superpig · Jul 29, 2014 at 02:32 PM

I'd flip it around - instead of saying "I want to raycast forward and see if I $$anonymous$$t the player", I'd say "I want to check whether the vector to the player is close to my forward vector."

Somet$$anonymous$$ng like t$$anonymous$$s:

 GameObject player = GameObject.FindGameObjectWithTag("Player");
 Vector3 vectorToPlayer = player.transform.position - sight.origin;

 // Calculate the vectorToPlayer as if it were flat and unit-length
 Vector3 flatDirToPlayer = 
     new Vector3(vectorToPlayer.x, 0, vectorToPlayer.z).normalized;

 // Check the angle is wit$$anonymous$$n 5 degrees
 if(Vector3.Angle(flatDirToPlayer, sight.direction) < 5f)
 {
     // Now do the raycast to check there's not$$anonymous$$ng in the way...
 }



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 raycosantana · Jul 29, 2014 at 05:08 PM 0
Share
avatar image
1

Answer by SilentSin · Jul 29, 2014 at 02:42 PM

Assuming you have access to the transform of the player:

 float heightDifference = player.position.y - transform.position.y;
 float horizontalDistance = Vector3.Distance(player.position, transform.position);

Now we have a right angled triangle:

  • Bottom length = horizontalDistance

  • Vertical side length = heightDifference

  • What we want is the hypotenuse to use as the ray direction; we still want to be pointing the triangle in the direction we are facing, but making sure we're looking at the right height.

Now we can start with the direction we are facing, then extend it out horizontally as the base of the triangle:

 sight.direction = transform.forward;
 sight.direction *= horizontalDistance;

Then raise it to the desired height:

 horizontalDistance.y = heightDifference;

Now we have a vector pointing where we are facing, aimed at the height of the player.

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 raycosantana · Jul 29, 2014 at 02:57 PM 0
Share

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

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

Related Questions

Setting exact Angle of a Raycast instead of using Direction 1 Answer

How to get the direction vector given a point and rotation? 1 Answer

bullets fly to the wrong direction in specific situations 0 Answers

Help with my "CanHitTarget" function 2 Answers

Numerical Comparison Logic 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