Camera Goes Through Things

I basically have a player with a camera following him with the smooth follow script. The problems is that when you turn a certain way, you can see through objects, like the houses. I know this is probably a really simple issue and I sound like a big noob right now (which I am) but I would appreciate anybody who could give me a start on this.

Thanks. :wink:

Our game just do a raycast from the player character towards the camera each frame.
It then checks if this position (the RaycastHit.point) is closer to the player, than the camera. If that’s the case, something must be between the player and the camera, and the camera will move to the position of the RaycastHit.point position.

But as I haven’t made the system myself, it may be I have the details wrong. And it only works if your objects has collisions, obviously… :slight_smile:

Good luck.

Sorry- I’m still learning scripting and haven’t done anything with raycasting yet.

So how would I at least start the script off?

Anybody?

Is it possible you are looking at the objects from the wrong side when this happens? (Like say moving inside a house and looking outwards?) Unity doesn’t display the reverse side of an object unless you specifically design it that way. Usually, the easiest way to do this is to make the original object double-sided in the 3D modelling app.

Sorry to bump this, but i have begun to try and fix this problem again, and this is what i have so far, attached to the main camera:

function Update () {
    var fwd = transform.TransformDirection (Vector3.forward);
    var up = transform.TransformDirection (Vector3.up);
    if (Physics.Raycast (transform.position, fwd, 2)) {
        transform.Translate(0,0,5);
    }
    if (Physics.Raycast (transform.position, -up, 2)) {
        transform.Translate(5,0,0);
    }
}

actually not working that bad, but i could use a little help improving it. Right now it moves through the object to get closer to the player, but i want it to move around the object. also, the movement is really sharp. I just really quickly typed this up, so its not much though :stuck_out_tongue:

I am not expecting anybody to write it for me either, just some tips would be nice :wink:

Thanks.

I’m bumping this because i really need some help. Anybody?

It sounds like you need your camera to have it’s own collision component associated with it and then react accordingly. Your problem will prove to be quite complicated though as 3rd person cameras are not ā€œsimpleā€ things to handle completely.

So, am i on the right path with the above script or is there a different method i should use?

The camera is shifted sideways quite a long way when the raycast detects an obstacle. You might want to try moving just a short distance. Since this will happen every frame, it will quickly move the camera out of the way, but will do so much more smoothly:-

transform.Translate(0,0,distancePerSecond * Time.deltaTime);

Multiplying the distance by Time.deltaTime will make the camera’s speed independent of the framerate.

Got this

function Update () {
    var fwd = transform.TransformDirection (Vector3.forward);
    
    var distancePerSecond = Time.deltaTime * 10;
    
    if (Physics.Raycast (transform.position, fwd, 2)) {
        transform.Translate(0,0,distancePerSecond);
    }
    
}

Now this:

function Update () {
    var fwd = transform.TransformDirection (Vector3.forward);
    var lft = transform.TransformDirection (Vector3.left);
    
    var distancePerSecond = Time.deltaTime * 5;
    
    if (Physics.Raycast (transform.position, lft, 1)) {
        transform.Translate (distancePerSecond,0,0);
    }
    
    if (Physics.Raycast (transform.position, -lft, 1)) {
    	transform.Translate (-distancePerSecond,0,0);
    }
    
    if (Physics.Raycast (transform.position, fwd, 2)) {
    	transform.Translate (distancePerSecond,0,0);
    }
    
}

Working pretty well, let me know what i can do to make it better.