- Home /
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.
@alucarddj Is this script free for use for commercial games?
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...)
@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.
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...
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
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.
Your answer
Follow this Question
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