- Home /
Camera Collision Help Thanks In Advance
Hello, all!
I have been making a developing a game with my friends, and we are pretty early into the development stage. I am one of the 3 programmers and we have been working on gameplay lately. But I recently noticed that the camera is going through walls and we need it fixed, but I don't know what to do.
But here's what I have tried:
I attached a collider component (didn't work) I attached a rigidbody component (didn't work) I attached made a new GameObject with a collider and rigidbody and had the camera be a child of it and vica versa I also made a C# script to make the camera follow the script:
using UnityEngine; using System.Collections.Generic;
public class FollowCamera : MonoBehaviour { public Transform CameraObject; using UnityEngine; using System.Collections.Generic;
public class FollowCamera : MonoBehaviour
{
public Transform CameraObject;
void Update()
{
// Move this transform towards the camera object.
transform.Set(CameraObject.position.z, CameraObject.position.y,
CameraObject.position.z);
}
}
}
The mouse orbit script I used looks like this:
var target : Transform; var distance = 10.0; var height = 5.0;
var xSpeed = 250.0; var ySpeed = 120.0;
var yMinLimit = -20; var yMaxLimit = 80;
private var x = 0.0; private var y = 0.0;
var heightDamping = 2.0; var rotationDamping = 3.0;
function Start () { var angles = transform.eulerAngles; x = angles.y; y = angles.x;
// Make the rigid body not change rotation if (rigidbody) rigidbody.freezeRotation = true; }
function LateUpdate () {
if (Input.GetMouseButton(1)) { if (target) { x += Input.GetAxis("Mouse X") xSpeed 0.02; y -= Input.GetAxis("Mouse Y") ySpeed 0.02; y = ClampAngle(y, yMinLimit, yMaxLimit);
var rotation = Quaternion.Euler(y, x, 0); var position = rotation Vector3(0.0, 0.0, -distance) + target.position;
transform.rotation = rotation; transform.position = position; var wantedRotationAngle = target.eulerAngles.y; var wantedHeight = target.position.y + height; var currentRotationAngle = transform.eulerAngles.y; var currentHeight = transform.position.y; currentRotationAngle = Mathf.LerpAngle (currentRotationAngle, wantedRotationAngle, rotationDamping Time.deltaTime); currentHeight = Mathf.Lerp (currentHeight, wantedHeight, heightDamping Time.deltaTime); var currentRotation = Quaternion.Euler (0, currentRotationAngle, 0); transform.position = target.position; transform.position -= currentRotation Vector3.forward * distance; transform.position.y = currentHeight; } } }
static function ClampAngle (angle : float, min : float, max : float) { if (angle < -360) angle += 360; if (angle > 360) angle -= 360; return Mathf.Clamp (angle, min, max); }
It looks like this:
But still nothing works. :-(
Any suggestions? And thanks in advance!
Regards, Ben
[EDIT]:
I have realized since the first post it was kind of vague so I included a screen shot of the game and attached the mouse orbit script I was using.
When you say that the camera is going through walls, do you mean you're able to walk completely through them as if they are not there, or do you mean that the camera clips the wall geometry when it gets too close to it? As is the camera renders the wall as if part of it is sliced off near the camera?
Yeah I have realized it was kind of vague, what's happening is the camera has the typical mouse orbit script that would come with Unity (only ours only orbits when you press the right mouse button) applied to it and the camera goes straight through the walls it looks really weird.
Its a 3rd person multiplayer game, although we haven't finished the multiplayer aspect yet and its based off of LEGOs it takes place in the the LEGO people's destroyed world and they are trying to rebuild it.
Seems pretty simple but I found an easy way to do it using Physics.Linecast, but thanks anyways!
Answer by whydoidoit · Oct 07, 2012 at 09:48 AM
You can try ray casting (or probably spherecasting) from the cameras location to the thing it is focussed on. If the raycast doesn't hit the focussed object first then leave the camera where it was in the last frame (both position and rotation). I normally allow a number of different potential camera angles and if one remains unavailable for a few frames I switch to another valid one.
Other techniques include approaching the focussed object by reducing the distance from the camera if it is currently obscured or changing the angle of the camera towards an overhead view
I used Physics.Linecast and it worked :D Thanks for the help.
Your answer
![](https://koobas.hobune.stream/wayback/20220613082006im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Camera gets flung off of the map when the player collides with certain objects. 0 Answers
Addforce to ball in camera position 0 Answers
How to use linecast ?? 1 Answer
AddForce to a point of a GameObject, not the center 1 Answer
Punching a crate 0 Answers