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 rauthelegendary · Sep 16, 2012 at 10:14 PM · cameraclipping

Corner of third person camera clipping through objects

Hello,

I have been working on a third person camera recently. I got the collision working quite easily, but I'm facing some issues with the corner of the camera clipping through objects. I have tried several things to fix it, but so far none have been succesful. Here is an example of what I mean, I've marked the issue with a red circle.

Clipping Issue

The camera is able to partly see through the wall, while it should be like in this demo: MasterCamera preview (use MouseVerticalAndHorizontal)

Here is my current code. Don't pay too much attention to the comments, I've been editing the code quite often and the comments probably aren't up to date anymore. Also, the code itself needs some cleanup as well. As I said, I've been editing it a lot and it's a bit of a mess right now. I've left out the end, but there is nothing remarkable there. I only set the definitive position there, which in the case of a collision, is based on the code you see here. The camera's near clipping plane is set to 0.1.

             //grab x and y values from the mouse
             x += Input.GetAxis("Mouse X") * rotSpeed;
             y -= Input.GetAxis("Mouse Y") * rotSpeed;
             //Make sure the Y value won't exceed 60 degrees. We don't want to be able to spin around the character on the y axis.
             y = Mathf.Clamp(y, -60, 60);
             
             //Is the camera looking up at the target?
             bool lookFromBelow = Vector3.Angle (transform.forward, target.transform.up * -1) > Vector3.Angle (transform.forward, target.transform.up);
             
             //set camera rotation based on the mouse movement
             rotation = Quaternion.Euler(y, x, 0);
             
             /* Here we calculate the position we would like the camera to be in. We do this by doing the following:
             We take the rotation we calculated above and multiply it by the direction we want the camera to face, which is always forward.
             Then we multiply it once more, but this time with the preferred distance to the target. The final attribute we need is the height
             at which the camera will be standing. We need this to be slightly upwards (which means we actually have to put down a negative float) otherwise the target would be blocking a large part of the screen.
             After all that we detract the result of this calculation from the targets position. This will give us the position of our camera. */
             Vector3 position = target.position - (rotation * Vector3.forward * preferredDistance + new Vector3(0, -1.0f, 0));
             
             //we'll use a raycast to check for any obstructions between the camera and its prey
             RaycastHit collisionHit;
             //we calculate the targets position taking into account the height of the target
             Vector3 targetPosition = new Vector3(target.position.x, target.position.y + 1.0f, target.position.z);
             
             //if there is an obstruction, correct the camera position and calculate the corrected distance    
             bool isCorrected = false;
             //set the corrected distance to the preferred one
             float correctedDistance = preferredDistance;
             
             
             //Just in case we missed something with the sphere cast we'll do a line cast as long as there was no correction by the sphere cast.
             if(!isCorrected && Physics.Linecast(target.position, position, out collisionHit, collisionLayer.value))
             {
                 //print ("Linecast collision");
                 Debug.DrawLine(target.position,collisionHit.point,Color.magenta); 
                 correctedDistance = Vector3.Distance(target.position, collisionHit.point) - wallOffset;
                 if(Input.GetMouseButton(0))
                 {
                     print (this.transform.rotation.x);
                 }
                 //print (correctedDistance);
                 isCorrected = true;
             }
             
             /*If the distance hasn't been corrected (no objects between camera and target) or the new corrected distance is larger than the current distance
             we use mathf.lerp to interpolate from the current distance back to the distance we actually want. We do this to ensure 
             that we smoothly go back from a zoomed in state*/
             if(!isCorrected || correctedDistance > currentDistance)
             {
                 //We use zoomDampening to allow for customization on the zoom speed
                 currentDistance = Mathf.Lerp(currentDistance, correctedDistance, Time.deltaTime * zoomDampening);
             }
             else
             {
                 //if there has been no correction or the corrected distance is less than the current one, we just set the current value to the corrected one straight away.
                 currentDistance = correctedDistance;    
             }


Thanks

clipping.png (126.7 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

0 Replies

· Add your reply
  • Sort: 

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

11 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

Related Questions

Shadow flicker 0 Answers

I cant see my gun its clipping through my camera 0 Answers

Fog Effect Horizon Flicker 0 Answers

How to use two cameras, one for culling one for rendering 2 Answers

Why does setting the Camera Clip Plane make my game run slow? 3 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