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
1
Question by PartyBoat · Feb 13, 2014 at 03:53 AM · vectorlinecast3rd person camera

How can I extend my linecast?

I have a basic idea about how to do this, but I'm having trouble with the math behind it.

Essentially, I have a camera that orbits around the player in a 3rd person view, and I'm implementing a system that moves the camera in closer if something gets in the way. I've figured out how to make this work. However, I want to extend the linecast past my end vector about half a unit so that I can create a little buffer between the camera and the wall to avoid clipping into it.

I can't simply subtract from the linecast distance, because it moves the camera away from the wall. The camera will Lerp back to the default distance, hit the wall, and move away again, creating a very annoying effect.

What I need is the unit vector of the linecast so I can use it to find a position vector 0.5 units from my camera. I want to use this as the endpoint instead, effectively "extending" the linecast behind my camera.

Here's a bit of my code for context:

 // Linecast to move the camera closer to the player if something gets in the way
 RaycastHit linecastInfo;
 if(Physics.Linecast(player.transform.position, Camera.main.transform.position, 
                     out linecastInfo))
 {
     // Ignore really short linecasts 
     //(buggy linecasts are constantly detecting hits at extreme close range, why?)
     if (linecastInfo.distance > 0.5f)
     {
         currentDistance = linecastInfo.distance;
     }
 }
 else
 {
     currentDistance = Mathf.Lerp(currentDistance, defaultDistance, 0.75f);
 }

BONUS: If anyone can tell me why my linecast is spazing out and constantly recording hits at extremely short distances (like 0.02 units, 0.004 units etc) even when nothing is obstructing the view, that would be great. Right now I have a pretty ugly hack in place to throw out any linecasts under 0.5 units in distance.

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

1 Reply

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

Answer by whydoidoit · Feb 13, 2014 at 03:59 AM

Well it's a pretty good reason to use a Raycast rather than a line cast - in either case you need the vector to the camera...

    var toCamera = Camera.main.transform.position - player.transform.position;
    var distance = toCamera.magnitude + 0.5f;
    var vector = toCamera.normalized;

    if(Physics.Raycast(player.transform.position, vector, out raycastHit, distance)) {
    }

I would imagine you are currently intersecting the actual player when you get your dodgy results - so perhaps:

   if(Physics.Raycast(player.transform.position + vector * 1, vector, out raycastHit, distance -1)) {
   }

Which offsets the start point.

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 PartyBoat · Feb 13, 2014 at 04:55 AM 0
Share

Thanks a bunch for the help on that. The first part is spot on. Unfortunately now I have a whole new problem. It looks like you were right about the raycast colliding with the player collider. I still don't understand why it would be so random. However, if I move the start point of the raycast far enough away so that it doesn't collide with the capsule collider at the highest camera angle, it misses walls that are close to the player at a more level camera angle. This wouldn't be an issue if I was using a sphere collider, but I'm not sure what to do about it for the different camera angles.

Is there perhaps a way to ignore the first collision? Although I'm not sure that would even work because the problem is so intermittent, and I'm not entirely convinced that's the root of the problem.

avatar image whydoidoit · Feb 13, 2014 at 05:17 AM 0
Share

So what I do is raycast from the camera to the centre mass of the player (the other way around to you) and then move the camera if the centre mass is obscured...

avatar image whydoidoit · Feb 13, 2014 at 05:23 AM 0
Share

I actually check if it hits the players collider in other words.

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

19 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

Related Questions

3rd Person Camera collision is jerky when zooming out 1 Answer

Camera Stutter During Distance Correction 0 Answers

Translate Vector3.forward and Raycast Vector3.forward have different directions 0 Answers

Tangent Space Normal Map to World Space Normal Vector in C# Script 0 Answers

Find intersecting points between vectors 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