Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by askull100 · Dec 08, 2015 at 05:36 PM · c#unity 5cameraraycastobstacle avoidance

Unity 5 - My raycast is hitting my player, and I can't figure out why

I'm working to get a camera that doesn't clip through objects. To achieve this, I'm using the following code:

     //Here I declare that my player's position will be the raycast start, and the camera's position will be the end
     Vector3 rayStart = target.position;
     Vector3 rayEnd = transform.position;

     //The ray direction will be normalized later, since we'll be basing the ray distance off of it
     Vector3 rayDir = rayEnd - rayStart;

     //We set the ray distance, and check to make sure the distance isn't 0 (the camera would be at exactly the same position as the player, in this case)
     float rayDist = rayDir.magnitude;
     if(rayDist <= 0.0f)
     {
         return;
     }

     //Now we normalize the ray direction, by dividing it by its magnitude
     rayDir /= rayDist;

     //Here, I make the actual raycast. It goes from start to end, in the correct direction, checks for a radius of 10.0f, and ignores everything except for the player and any layer labelled as "Ignore Raycast" (at least, it should). If we didn't hit anything, we return, since there's nothing to do.
     RaycastHit[] hitInfos = Physics.SphereCastAll(rayStart, ObstacleCheckRadius, rayDir, rayDist,  m_RaycastHitMask);
     if(hitInfos.Length <= 0)
     {
         return;
     }

     //Set the minimum move up distance. If the new distance is smaller than the old distance, we don't bother putting it in. In other words, we're looking for the closest object. 
     float minMoveUpDist = float.MaxValue;
     foreach(RaycastHit hitInfo in hitInfos)
     {
         GameObject obstacle = hitInfo.collider.gameObject;
         minMoveUpDist = Mathf.Min(minMoveUpDist, hitInfo.distance);
     }

     //Now apply that distance to our camera.
     if(minMoveUpDist < float.MaxValue)
     {
         transform.position = rayStart + rayDir * minMoveUpDist;
     }

     return;

All of this should be working, but the camera is being set to my player's position. I'm wondering why, and I'm almost positive it's a layer mask problem. Here's the mask I use:

   m_RaycastHitMask = ~LayerMask.GetMask("Player", "Ignore Raycast");

It ignores everything except for the Player and Ignore Raycast layers. All of my game objects which shouldn't be hit are set to either of these, but it still registers the player as an obstacle. Any ideas on why this is?

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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by PedroDev · Mar 25, 2016 at 04:00 PM

just ignore the Tag of your player ..

 if (MyRaycastHit.Collider.gameObject.Tag != "Player" ){
     // Do your things ... 
 }


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 ZakDaBeast23 · Nov 14, 2020 at 02:42 PM

I got 56 errors from 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

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

uNet hitboxes and movement. 0 Answers

Camera isn't move position? Why my camera isn't change position? 0 Answers

Problem of stuttering FPS Camera 1 Answer

How To Make The Camera turn around the player using the mouse only separatly from character movement? 0 Answers

How to make a "raycast" vertically in front of the player ? 0 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