- Home /
Ray/line casting for camera angle
I am trying to get the main camera (the only camera in the scene) to follow a sphere. Easy enough. The problem is that I want the cameras height above the sphere to stay the same (relative to the platform on which the sphere is traveling). I know little (even that is an overstatement) about raycasting/linecasting, but that is what I am attempting to use. Now, if you know any better way to adjust the camera height based on surface that the character is traveling, please let me know. If there is a better way or even just a way that works, I would appreciate it if you would post it. Oh, just as background info, there is an empty object is constantly updating it's position to that of the sphere.
First off, let's deal with when the sphere is rolling down a down-angled platform. If the platform is angled down enough, the camera will be behind the platform (see my camera code):
Camera.main.transform.position.z=transform.position.z-7.0;
Camera.main.transform.position.x=transform.position.x+0.0;
Camera.main.transform.position.y=transform.position.y+1.0;
Now that's a problem! What I attempted to do was to draw a line between the sphere and the camera:
if (!Physics.Linecast (transform.position, Camera.main.transform.position)) { print ("good!"); }
Debug.DrawLine(transform.position, Camera.main.transform.position, Color.white);
I am assuming that the debug.drawline is the same as the actual line. Ok, so I run this code (in FixedUpdate) while the sphere is rolling down an angled plane. The camera is underneath the plane and the white line (from debug.drawline) goes through the plane to the camera, but the code inside the if statement is NOT called. What am I missing? Although the camera, sphere, and empty object on the sphere are layerd "ignore raycast", the plane is "default".
if(Physics.Raycast (follower.transform.position, -Vector3.up, hit, 4.0)){}
Debug.DrawRay (follower.transform.position, -Vector3.up *4, Color.red);
Is the debug.drawray the same as the actual ray? Assuming it is, this casts a ray from an object that is -4 units below the camera (on the y axis). The goal is to have the camera raise (when there is something between the sphere and the camera) until this ray hits something (hopefully the platform that the sphere is on). Of course, there could be other platforms, the platform could be think, etc. Thus, this plan is kind of rudimentary or even useless.
Anyway, I have more code for detecting if the sphere is going up an up-angled platform, but I think that I have thrown enough problems out there for now.
Thanks in advance!
-Robert
Answer by AppTechStudios · Mar 07, 2011 at 03:12 AM
Ok, I think that I got it all to work! I'll try to explain it as I go as best as I can.
This is in the FixedUpdate function:
var ballRay : RaycastHit; //This is a var (posBack) that holds a direction that is -1 meters on the y axis and -.5 on the z //axis. Basically, just a direction that is diagonally backwards down relative to the z axis of the object. var posBack : Vector3 = transform.TransformDirection (0,-1,-0.5); // Just a variable that holds the position of the main sphere (the script that this object is //attached to is constantly updating its position to that of the sphere, so using either transform.position or the sphere's position would work location=ballFind.transform.position;
//if the sphere is not "dead" and is touching a surface (this is from another script using //OnCollisionEnter/Exit)
if(BallMove.aliveTouch==1){
//Cast a ray from location (either the sphere's location or transform.position) diagonally backward (posBack). ballRay simply holds variables like the distance between the origin of the ray and it's initial hit point
Physics.Raycast (location, posBack, ballRay);
//A ray that is visible in the editor for visual reference Debug.DrawRay(location, posBack, Color.green); //Just checking the distance between the origin of the ray and where it initially hit (whatever platform it is rolling on). For my game, .54 is traveling on a level surface
if(ballRay.distance>.54){ //This is setting camHeight, or what the camera should be. Lower down we gradually adjust the actually height of the camera to be just that. camHeight=5.158-ballRay.distance*7.7; } if(ballRay.distance<.54){ camHeight=23.518-ballRay.distance*41.7; } //Make the camera height equal to that of the variable that we use below. Camera.main.transform.position.y=transform.position.y+curCamH; }
And in the Update function:
if(BallMove.aliveTouch==1){
//If the current camera height is more than .1 units bigger than what it should be...
if(curCamH>camHeight+0.1){
//Gradually raise the height of the camera (y axis position)
curCamH-=2.5*Time.deltaTime;
}
if(curCamH<camHeight-0.1){
curCamH+=2.5*Time.deltaTime;
}
}
I hope that this helps someone else, it certainly wasn't an easy problem to get over (mostly getting the ray to work correctly). Of course now it is a piece of cake ;)
Your answer
Follow this Question
Related Questions
3rd Person Camera collision is jerky when zooming out 1 Answer
Trying to fix bouncy linecast camera collision 1 Answer
Raycast hitting objects to the left of my player 1 Answer
Ray Cast not working for camera. 1 Answer
Raycast in camera 1 Answer