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 darkhog · Nov 07, 2014 at 12:00 AM · camerathird-person

Problems implementing TPP camera that doesn't go through walls

I am trying to develop third person camera that doesn't go through walls and so far I'm failing at it miserably ;(.

First I've got code from some site with Unity snippets and tutorials (don't remember URL, although if you'll google code, you should find it). Of course, as most solutions that are available for free on the web, this camera passed through walls. My friend suggested that I cast ray from player in direction of camera with length roughly same as distance between the two, and if collide with something, put camera on this point instead of maximum one. But unfortunately code that puts camera at point of ray collided with object is somehow borked, as camera upon collision with wall gets something that can only be called epileptic surges.

Here's the code:

 using UnityEngine;
 public class FollowCam : MonoBehaviour {
     public GameObject target;
     public float rotateSpeed = 5;
     Vector3 offset;
     float basedistance;
     public float mindist;
     void Start() {
         offset = target.transform.position - transform.position;
         basedistance = Vector3.Distance(target.transform.position, transform.position);
     }
     
     void LateUpdate() {
         float horizontal = Input.GetAxis("Mouse X") * rotateSpeed;
         target.transform.Rotate(0, horizontal, 0);
         Ray ray = new Ray();
         ray.origin=target.transform.position;
         ray.direction = this.transform.position;
         RaycastHit rayhit = new RaycastHit();
         if (!(Physics.Raycast(ray,out rayhit,basedistance*2))){
             float desiredAngle = target.transform.eulerAngles.y;
             Quaternion rotation = Quaternion.Euler(0, desiredAngle, 0);
             transform.position = target.transform.position - (rotation * offset);} 
         else {
             float desiredAngle = target.transform.eulerAngles.y;
             Quaternion rotation = Quaternion.Euler(0, desiredAngle, 0);
             transform.position = target.transform.position - (rotation * rayhit.point);
         }
         
         transform.LookAt(target.transform);
     }
 }


and here's a gif of what happens after camera collides with wall: https://dl.dropboxusercontent.com/u/210143/padaka.gif (no, after making ray collide with a wall, I didn't move mouse any further).

As for basedistance*2, it is that way, because I've seen that camera passes through wall a bit before going into epileptic surge which meant ray didn't hit even though it should. That seemed to fix that issue, leaving epilepsy of camera.

Could someone help me? I've tried to fix this code however I could and nothing I did helped.

Comment
Add comment · Show 5
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 AlucardJay · Nov 07, 2014 at 02:24 PM 1
Share

http://wiki.unity3d.com/index.php?title=SmoothFollowWithCameraBumper

avatar image darkhog · Nov 08, 2014 at 01:24 AM 0
Share

@alucarddj Is this script free for use for commercial games?

avatar image MrSoad · Nov 08, 2014 at 01:32 AM 0
Share

Yes it is, if you scroll to the bottom of the page click on the link " Creative Commons Attribution Share Alike" and it will take you here :

http://creativecommons.org/licenses/by-sa/3.0/

Which says you can do whatever you want provided you give appropriate credit and allow others free access to whatever you do with it(that will not mean your whole game, just any improvements/additions you make to this particular script etc...)

avatar image darkhog · Nov 08, 2014 at 01:43 AM 0
Share

@alucarddj anyway, there are some problems with this script. First of all, I want to rotate camera around with mouse and object rotate along with it. Secondly, when rotating, I don't want camera to rotate on x/z if the object is tilted.

avatar image MrSoad · Nov 08, 2014 at 02:00 AM 0
Share

That is a different question, I would accept alucarddj's comment as an answer and thank him for his efforts, then try to adapt this script yourself to do what you want it to do...

2 Replies

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

Answer by AlucardJay · Nov 08, 2014 at 05:46 AM

I offered that link as an example for combating Camera Occlusion (with a method as suggested by your friend). For the complete solution you are looking for, there are other solutions you could borrow from and/or adapt to your needs.

Unitys 3rd Person MMO Controller and Camera : https://www.assetstore.unity3d.com/en/#!/content/917

Forum WOW Camera : http://forum.unity3d.com/threads/wow-camera-movement.16949/

Searching 'Unity WOW Camera' comes up with a few results worth investigating.

My personal favourite is the 3rd Person Character Controller by 3DBuzz. Complete this course and you will have a very robust Camera Controller with good anti camera occlusion : http://www.3dbuzz.com/training/view/3rd-person-character-system

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 MrSoad · Nov 07, 2014 at 12:41 AM

A simple method would be to put a trigger on the camera. If it makes contact with a wall zoom in. Have a second trigger right behind it, if this one is not colliding with anything and the camera trigger is not then you can zoom out again until either the second trigger gets contact or the camera reaches its maximum zoom.

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

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

Related Questions

3rd person camera reset bug 0 Answers

Third Person Camera Wall Covering Camera 0 Answers

Problem with mouse look and second skybox cam 0 Answers

3rd person clipping 0 Answers

Camera movement in a Third Person Shooter Game 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