In making the interface for my ORPG Avatar of Destiny I came across the ugly problem of the camera clipping through the terrain. After extensive searching I could find nothing on getting it to happen so I spent some time figuring out a relatively simple method to implement it.
This is a basic tutorial on getting the camera to collide with the terrain as well as non-pathable portions of the terrain.
The most important thing is getting the camera height. This is done by some simple math:
(CameraPitch/90)*CameraDistance=CameraHeight
One thing to note that in SC2 the cameras pitch when it sinks below 90 degrees does NOT go to -1 it goes to 360 instead. So we have to account for this by subtracting 360 from it IF its over 90 degrees (I did 180 on the test map just in case).
Now that we know how to get the camera height we can do a simple trace.
CameraCollisionOptions:Action,CreateThreadReturnType:(None)ParametersPlayer=0<Integer>GrammarText:CameraCollision(Player)HintText:(None)CustomScriptCodeLocalVariablesTargetPoint=NoPoint<Point>DistanceMax=20.0<Real>DistanceChange=(DistanceMax/40.0)<Real>CameraHeight=0.0<Real>Pitch=0.0<Real>Yaw=0.0<Real>TempDistance=DistanceChange<Real>ActionsVariable-SetTargetPoint=(PositionofUnit)Variable-SetYaw=(180.0+(CurrentcamerayawofPlayer))Variable-SetPitch=(CurrentcamerapitchofPlayer)General-If(Conditions)thendo(Actions)elsedo(Actions)IfPitch>180.0ThenVariable-ModifyPitch:-360.0ElseVariable-ModifyPitch:/90.0//The 0.5 is how high off the ground the Cameras Target point is.Variable-SetCameraHeight=(0.5+(GroundheightatTargetPoint))General-While(Conditions)aretrue,do(Actions)ConditionsTempDistance<DistanceMax//The Or here causes it to collide with non-Pathable terrain but not if theres a cliff below you (note: this WILL collide with the very edge of a cliff at times)OrConditions((TargetPointoffsetbyTempDistancetowardsYawdegrees)ispassable)==true(ClifflevelofTargetPoint)>(Clifflevelof(TargetPointoffsetbyTempDistancetowardsYawdegrees))(CameraHeight+(Pitch*TempDistance))>(0.25+(Groundheightat(TargetPointoffsetbyTempDistancetowardsYawdegrees)))//The 0.25 added to the ground height is how high off the ground the camera should float (to avoid clipping into foliage etc)ActionsVariable-ModifyTempDistance:+DistanceChangeCamera-ApplycameraobjectDistance(TempDistance-DistanceChange)forplayerPlayerover0.2secondswith100%initialvelocityand0%deceleration
We have it cycle through to find if the cameras height falls below the terrain height, if it does the while loop stops and the cameras distance is set to that distance.
In making the interface for my ORPG Avatar of Destiny I came across the ugly problem of the camera clipping through the terrain. After extensive searching I could find nothing on getting it to happen so I spent some time figuring out a relatively simple method to implement it.
This is a basic tutorial on getting the camera to collide with the terrain as well as non-pathable portions of the terrain.
The most important thing is getting the camera height. This is done by some simple math:
One thing to note that in SC2 the cameras pitch when it sinks below 90 degrees does NOT go to -1 it goes to 360 instead. So we have to account for this by subtracting 360 from it IF its over 90 degrees (I did 180 on the test map just in case).
Now that we know how to get the camera height we can do a simple trace.
We have it cycle through to find if the cameras height falls below the terrain height, if it does the while loop stops and the cameras distance is set to that distance.
@Sneakervek: Go
This could prove to be quite useful. I hope you don't mind if I uh... "borrow" this for my own ORPG?
@Nebuli2: Go
I wouldn't post it if I didn't want people to use it XD
@Sneakervek: Go
Excellent.
Moved to tutorial section
Contribute to the wiki (Wiki button at top of page) Considered easy altering of the unit textures?
https://www.sc2mapster.com/forums/resources/tutorials/179654-data-actor-events-message-texture-select-by-id
https://media.forgecdn.net/attachments/187/40/Screenshot2011-04-17_09_16_21.jpg