Hey, all. I made a script I thought I would share with everyone. I took the collision detecting camera script posted on one of the Unity wikias and fixed it plus modified it to work with Ork. What you have to do is set a tag on the game object the camera is supposed to follow, then type the name of the tag in to the script options in Unity, the tag is set to Player by default. It waits a second before it starts to allow ORK to spawn the player, and then voila, your camera won't pass through walls and objects (as long as they have a collider). When a collider is between the camera and target, it moves the camera to just ahead of the collider. You can also use the mouse scroll wheel to move the camera closer or further away for zooming in and out.

To enable it on your camera just type in whatever name you save the script under as a camera behavior like sopothetocho said below and it will work with ORK.

var TargetTag : String = "Player";
private var target : GameObject;
var distance = 5.0;
var xSpeed = 250.0;
var ySpeed = 120.0;
var yMinLimit = -20;
var yMaxLimit = 80;
var distanceMin : float = 1;
var distanceMax : float = 5;
private var x = 0.0;
private var y = 0.0;
private var zoom : float;


function Start () {
zoom=distanceMax;
yield WaitForSeconds(.1);
target = GameObject.FindWithTag(TargetTag);
var angles = transform.eulerAngles;
x = angles.y;
y = angles.x;
if (rigidbody) rigidbody.freezeRotation = true;
}

function Update () {
if (target) {
x += Input.GetAxis("Mouse X") * xSpeed * 0.02;
y -= Input.GetAxis("Mouse Y") * ySpeed * 0.02;
y = ClampAngle(y, yMinLimit, yMaxLimit);
var rotation = Quaternion.Euler(y, x, 0);
zoom = Mathf.Clamp(zoom - Input.GetAxis("Mouse ScrollWheel")*5, distanceMin, distanceMax);
var SuggestedPos = rotation * Vector3(0.0, 0.0, -zoom) + target.transform.position;
var hit : RaycastHit;
if (Physics.Linecast (target.transform.position, SuggestedPos, hit)) distance = Mathf.Lerp(distance, hit.distance-.5, .25);
else distance = Mathf.Lerp(distance, zoom, .1);var position = rotation * Vector3(0.0, 0.0, -distance-.5) + target.transform.position;
transform.rotation = Quaternion.Slerp(transform.rotation, rotation, 10);
transform.position = Vector3.Lerp(transform.position, position, 10);
}
}


static function ClampAngle (angle : float, min : float, max : float) {
if (angle < -360)
angle += 360;
if (angle > 360)
angle -= 360;
return Mathf.Clamp (angle, min, max);
}
Post edited by Ntelliware on
  • Thanks man, that's real nice of you to post this. I'll try and hook it up in my game at some point and let you know how it goes.
  • You're welcome, I just didn't think it would be right to try and sell it since it was based on a free script, and saw no need to hoard it to myself.
  • edited December 2013
    Yeah, I can understand where you're coming from and again, it's real nice of you to post it :)

    It works pretty good man, once you tweak the settings to how you want them. I haven't been able to block the camera control yet though as I haven't really looked into using camera commands in the events. I'll read up on how to disable/enable mouse orbit in the Ork documents at some point. It would be probably more ideal to insert coding for that, although at this time I don't believe the source code for scripts is available (if that's even correct, I may be wrong about needing code to do it in a more natural way). I couldn't get the Main Camera to change positions either but I'm going to conclude that's simply because I haven't blocked the control for the cam yet. Besides that can be done via other cameras.

    Thanks again man.
    Post edited by Loopdaloop on
  • All you have to do to disable the camera is add an actor to the event, set the camera as the actor, and add a Function Steps->Enable Component step at the start and end, each one set to enable/disable Mouse Orbit (or whatever else you name the script) on the actor you assigned the camera to, just remember to add an Enable Component step at the end to turn it back on.
  • Gotcha, thanks for the tip.
    I dunno if you can make use of these or not but I had a health and oxygen bar scripted by a freelancer a while back. They have a few features such as appearing and vanishing after so much time without being underwater/getting hurt and some other stuff, can't really remember.
    I'm not using them anymore but if you or anyone else can make use of them, feel free to grab 'em up.

    http://www.filedropper.com/health-oxygen_1
  • There's no need to add an event, to block the camera during combat simply add the behavior in the Base/Control-Game control options, where it says "camera control" press "add behavior" and type the exact name of the script there (remember also to select "none" in camera control/type), you can also add the script on runtime by checking "Add component", no need to add the script to the camera before hand.
  • Ok, so I tried it sopothetocho's way and it does work. Add it as a behavior under camera controls as he said, and when an event blocks camera control the script is temporarily disabled, makes it a lot easier! Thanks, Sopo!
  • edited December 2013
    Yeah, got it hooked up now too. :) Ty both
    Post edited by Loopdaloop on
  • edited May 2014
    Just updated the script in the original post, I finally got around to perfecting it, now instead of slowly moving the camera back to distanceMax, it will quickly and smoothly snap back to the current zoom level. So if you have it zoomed where you like it, it will stay there, and if you brush up against a wall it will zoom in and then zoom back to where you like it after you move away from the wall. Enjoy!
    Post edited by Ntelliware on
  • Thanks mate for sharing! :)
Sign In or Register to comment.