- Home /
3rd Person Camera Collision
I've been working with Unity for some while to try to create basic games. Lately, I've been running into a problem. I'm using the script in the 3D Platform Game for my camera and character. I like how it works, but lately, I've been running into a problem with the camera. It's great, but when I run into a hill in the terrain, you can see through it which kinda ruins the effect. I've seen ray casting done on this stuff, but I"m a n00b at this, and the scripts other's supply don't really get that effect I want. Thanks in advance if you can help :D
Answer by Tetrad · Jun 18, 2010 at 01:41 AM
This is untested, but should get the point across. I've ripped apart a script that we're using in the game here but with the mouse wheel (or -/= keys) move in/out stuff taken out.
Just make a new class (C#) and drop this in there and attach it to your camera object.
For this script I assume that the hierarchy is something like player base (ground) -> player shoulder-ish -> camera (placed away from the shoulders kind of like a dolly).
public float minDistance = 1.0f; public float maxDistance = 10.0f;
Vector3 dollyDir; float distance;
void Awake() { dollyDir = transform.localPosition.normalized; distance = transform.localPosition.magnitude; }
void Update() { Vector3 desiredCameraPos = transform.parent.TransformPoint( dollyDir * distance );
// (optional) put layers you don't want to collide with here (probably things like enemies)
const int ignoreLayer1 = 1 << 18;
const int ignoreLayer2 = 1 << 19;
int layerMask = ignoreLayer1 | ignorelayer2;
layerMask = ~layerMask;
RaycastHit hit;
if( Physics.Linecast( transform.parent.position, desiredCameraPos, out hit, layerMask ) )
{
distance = Mathf.Clamp( hit.distance, minDistance, maxDistance );
}
transform.localPosition = dollyDir * distance;
}
Well, I'm not so smart when it comes to C# so can you be a bit more specific on how I should do this. I'm not used to C# scripting
Just put that code in a class and attach that script to whatever holds your camera. That's assu$$anonymous$$g that the camera is a child of your player in the hierarchy.
When I apply the script it says
"Assets/ThirdPersonCamera.cs(24,40): error CS0103: The name `ignorelayer2' does not exist in the current context"
It's a typo, and meaning to be an example. Just remove that layer stuff.
For some reason, it just won't work. The scripts I'm using are the ones in the 3D Platformer Tutorial from Unity3d.Com
Answer by increpare · Jul 25, 2010 at 12:22 PM
Here's a slightly tidied-up/tested version of Tetrad's C# script - it zooms smoothly back out after you get too close to a wall (the other one stays close to your character after going towards a wall and back). Just need to attach it to a camera (that's parented to the object being moved).
using UnityEngine; using System.Collections;
public class CameraCollision : MonoBehaviour {
public float minDistance = 1.0f;
public float maxDistance = 4.0f;
public float smooth = 10.0f;
Vector3 dollyDir;
float distance;
void Awake()
{
dollyDir = transform.localPosition.normalized;
distance = transform.localPosition.magnitude;
}
void Update()
{
Vector3 desiredCameraPos = transform.parent.TransformPoint( dollyDir * maxDistance );
RaycastHit hit;
if( Physics.Linecast( transform.parent.position, desiredCameraPos, out hit ) )
{
distance = Mathf.Clamp( hit.distance, minDistance, maxDistance );
}
else
{
distance=maxDistance;
}
transform.localPosition=Vector3.Lerp(transform.localPosition, dollyDir * distance, Time.deltaTime * smooth);
}
}
Hey, i'm combining this script with the $$anonymous$$ouseOrbit script but the colisions aren't working....
$$anonymous$$ee too, maybe you should edit mouseorbit script to make it work
These should be separate scripts, this one's C# and the default $$anonymous$$ouseOrbit is Javascript..
Answer by Code_Monkey_Refurbished · Jan 12, 2015 at 06:54 AM
or...
private void compensateForWalls(Vector3 fromObject, ref Vector3 toTarget)
{
Debug.DrawLine (fromObject, toTarget, Color.cyan);
RaycastHit wallHit = new RaycastHit ();
if(Physics.Linecast(fromObject, toTarget, out wallHit))
{
Debug.DrawRay(wallHit.point, wallHit.normal, Color.red);
Vector3 wallHitVector3 = new Vector3 (wallHit.point.x, wallHit.point.y, wallHit.point.z);
toTarget = new Vector3 (wallHitVector3.x, toTarget.y, wallHitVector3.z);
}
}
Your answer
Follow this Question
Related Questions
Jittery Collision Detection 0 Answers
3rd person camera that avoids objects 0 Answers
How to keep the player on the left side o the screen? 2 Answers
Prevent camera from going trough objects? 0 Answers
3rd person camera through wall 2 Answers