• 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 CStunner · Jul 11, 2015 at 11:00 AM · wall

stop camera from going through walls

I know it's been asked before, and i didn't find a good answer so i am asking it again, how to stop camera from entering walls in a 3rd person shooter game.. if you have played Manhunt or even Gta Sa, you'll find it smoothly lerps above or very close to the Player..

i don't like the idea of collider + physics material, it didn't work as i wanted.. and i did make my own code.. it casts a ray at the front, checks if player is hit, and moves forward or else raycasts at back and adjusts the position at the player's back..

it works.. but there are 3 problems i'm having:

  1. it works only AFTER the camera has actually entered the wall (In Manhunt 1 and Gta games, the camera AVOIDS the walls altofether)

(I did try Collider + smooth physics material) WITH and WITHOUT my raycasting code, but it's not helpful

  1. At certain Angles, the Raycasting doesn't work, maybe it's just my Editor's POV, but it looks like it's in already.

  2. my raycasting code makes the camera shake obviously because it's getting [hit..not hit.. hit.. not hit..] conditions match back and forth)

I'm still unable to solve this issue, there's no smooth avoidance of the walls, do you have any better approach to solving this?

Thanks

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

· Add your reply
  • Sort: 
avatar image
3

Answer by CStunner · Jul 12, 2015 at 01:56 PM

Okay, i solved this, for anyone who might face this problem in the future, there are 2 ways to solve this:

  1. Read code from 3rd person character controller + fly mode from asset store and read the DoubleViewPosCheck function in ThirdPersonCamOrbit script file (this one is a bit complicated and the camera is NOT a child of the Player in this project)

  2. Use my method:

    float raycastDistance = 15f;

    float spherecastDistance = 5f;

    float sphereRadius = 2f;

    float speedToFixClipping = 0.2f;

    void FixClippingThroughWalls () {

      RaycastHit hit;
    
         Vector3 direction = transform.parent.position - transform.position;
    
         Vector3 localPos = transform.localPosition;
    
         
         for (float i = offset.z; i <= 0f; i += speedToFixClipping)
         {
             Vector3 pos = transform.TransformPoint (new Vector3 (localPos.x, localPos.y, i));
             if ( Physics.Raycast (pos, direction, out hit, raycastDistance))
             {
                 if (!hit.collider.CompareTag ("Player"))
                     continue;
                 
                 if (!Physics.SphereCast (pos, sphereRadius, _transform.forward * -1, out hit, spherecastDistance)) {
                     localPos.z = i;
                     break;
                 }
             }
         }
         transform.localPosition = Vector3.Lerp (transform.localPosition, localPos, smoothing * Time.deltaTime);
     }
    
    
    

Code Explanation: It's pretty simple, we store the local position in a local variable and just use a for loop to reduce the local z position -- from Camera's predetermined Z offset (-3.7f) (Read NOTE) down to 0f, at each iteration we just raycast from the front (to make sure the Player is in front of the Camera) and a spherecast from the back (raycast causes more jerking), we settle at a position where the Player is in front and there is NO collider hit at the back ..

NOTE: The Camera is a child of Pivot Point which is child of the Player in my character, you might need to tweak the radius and spherecast distance to prevent jerking of Camera.

The Pivot Point's LocalPosition is: 0f, -0.3f, 0f and the Camera's Local Position is: 0f, 1.6f, -3.7f in my case, in case you've a different local distance, you'll need to tweak those to fix the jerking..

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
1

Answer by tanoshimi · Jul 11, 2015 at 11:01 AM

Intelligent camera behaviour is complicated, and I highly doubt you're going to get a definitive answer on a Q&A style site like this. Bear in mind that games like GTA often have a programmer dedicated to doing nothing other than programming the camera.

The following Gamasutra article is nearly 6 years old, but still contains a lot of useful alternative you might want to consider: http://www.gamasutra.com/view/feature/132456/realtime_cameras__navigation_and_.php?print=1

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 omarahmedabdelhakim · Dec 29, 2020 at 05:30 PM

Ez Pz, Just change the field of view in the camera's inspector.

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.

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

3rd Person Ladder/Wall Climb Script 1 Answer

Simple wall jump? 2 Answers

How to destroy the cube which I selected with the arrow key 2 Answers

Rigid bodies pushable 2 Answers

Trash Collection for Instantiated Objects 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