- Home /
Camera Collision with Linecast not completely working
Hey, I am having a few small problems that I am pretty much out of ideas for fixing. here is the code before anything:
using UnityEngine;
using System.Collections;
public class Camera_beh : MonoBehaviour {
Transform target;
float zMov = 8.0f;
void Start () {
target = GameObject.Find("Player").transform;
}
void LateUpdate () {
RaycastHit hit;
if(Physics.Linecast(target.position, transform.position, out hit)){
zMov = hit.distance;
}else{
zMov = 8.0f;
}
Vector3 tempV = new Vector3(0, 0, -zMov);
transform.localPosition = tempV;
}
}
This is the script I have currently attached to my camera. The camera is a child of a child of the character. The character has a transform child (what the camera is a child of) which sits just above the character and is what i manipulate to rotate the camera in a third person environment. In other words, the camera has no movement or rotation manipulation on it, it follows its parents movement and rotation. This is the only code I have on it and it is for detecting collision objects in front of it, then it moves in front of the collision. The camera has no mesh collider or rigidbody attached to it. Finally the problem I am having is that when the camera is forced into a bottom edge where a wall meets the floor, it starts to flicker. This also happens on a certain angle against a normal wall too. My only guess could be that the camera is trying to go back to its default setting (-8.0) but I have debugged the linecast (hit.distance) to be constant and not flickering. Thankyou
Answer by TonyLi · Apr 01, 2013 at 02:07 PM
It's probably bouncing between linecast hits as zMov changes. You'll get more reliable behavior with five linecasts originating from your character: one through each corner of the near clip plane, and one through the center and a little past the back of the camera. The corner linecasts will catch objects that only partially obscure one side of the camera view but aren't blocking the center.
The guys at 3DBuzz did a great tutorial on building a camera:
http://www.3dbuzz.com/training/view/3rd-person-character-system/simple-character-system
The camera-specific videos start here:
Scott Rogers, in Level Up!, emphasizes the importance of the 3 C's (character, control, and camera). It's definitely worth it to make your camera as robust as possible, and the 3DBuzz tutorials have a lot of good ideas.
Your answer
Follow this Question
Related Questions
C# - Offset In-Air Camera from Current Object Position 1 Answer
vector3.forward doesn't work when used in Rigidbody? 2 Answers
Camera moves into Object instead of following 1 Answer
nullifying targetObject after a Smooth LookAt Transition 2 Answers
What is difference between local and global coord Vector3. 1 Answer