|
hey guys. i have a camera movement c# script that follows the player and also has mouse orbit. however, the camera goes through the terrain and buildings and i want to fix that. here's the script: using System.Collections; public class CameraMovement : MonoBehaviour { public Transform target; public float walkDistance; public float runDistance; public float height; public float xSpeed = 250.0f; public float ySpeed = 120.0f; public float heightDamping = 2.0f; public float rotationDamping = 3.0f; // _myTransform.position = new Vector3(target.position.x, target.position.y + height, target.position.z - walkDistance); // _myTransform.LookAt(target); if(target != null) { if(_camButtonDown) { // _myTransform.position = new Vector3(target.position.x, target.position.y + height, target.position.z - walkDistance); // _myTransform.LookAt(target); _x = 0; _y = 0; } i know its a long script, but maybe someone can give some code in c# that keeps the camera from going through the terrain. thanks.
(comments are locked)
|
|
Your basic task is to position the camera above the terrain at the point it is located. You can get the terrain height from the terrain object. To stop it intersecting buildings you need to ray cast from the camera and see if it hits something before your character - then if it does you either zoom in or move the camera in some other way. Check out the orbitcamera script on my blog. I might add, it's a worthwhile technique to add an empty game object called PutativeCameraPosition and attach to that a behaviour rather as you currently describe. Then, make another empty game object called LessPutativeCamerPosition Or perhaps PutativeButImprovedByMikesCodeCameraPosition attach to that a behaviour which essentially does all that Mike generously tells you here and in his popular blog. Finally have an empty game object called CameraTripod which "merely" follows PutativeButImprovedByMikesCodeCamerPosition (BUT if this is anything other than a hello world test, you'll ultimately have a whole lot of bollocks in there regarding smoothing network sawtoothing blah blah) finally just attached your camera rig under that final object. I encourage people to generally separate out "marker" objects like this. Also generally to take a behaviour-ish approach to video games. (Write in a behaviourish manner just to be cool - there's no other reason really. If you're as smart as Mr Data from Start Trek TNG, you can just write perfect spaghetti code, even over time domain, and everything will work perfectly. If you can do that, do it and get paid and go home to supper with the kids. This is where the catchphrase "Mr Data just writes spaghetti code" comes from. Me, I'm so dense I adhere to the "no function over two lines long" comp sci dictum.)
Jun 20 '12 at 12:07 PM
Fattie
Nice suggestion :)
Jun 20 '12 at 05:18 PM
whydoidoit
i went on mike's blog and found the orbit camera script, but its saying it cant find the url. is there another way besides these that will work?
Jun 22 '12 at 11:58 AM
BakuJake13
My old file host was a pig - the download should work now...
Jun 22 '12 at 01:50 PM
whydoidoit
i tried it out in unity, but the camera just floated up from the ground. is there a way i could add some code to my camera movement script?
Jun 22 '12 at 02:09 PM
BakuJake13
(comments are locked)
|
