hello everybody,
I’m trying to make an application with a friend that allows us to virtually disect an anatomical object (for example this hand model: http://i.imgur.com/evZqizu.jpg)
We are still in early progress, working on the camera movement:
Rotating/zooming is working fine, but we are struggling how to keep the model within screen edges (preferably to let it stop moving at the screen edges so it will be impossible to get lost).
We are using camera movement for the dragging/panning of the model, using this script:
var moveSpeed : float = 0.1;
static var main: Camera;
function start()
{
}
function Update ()
{
if (Input.GetMouseButton(1)) {
transform.Translate(Vector3.right * -Input.GetAxis("Mouse X") * moveSpeed);
transform.Translate(transform.up * -Input.GetAxis("Mouse Y") * moveSpeed, Space.World);
transform.position = new Vector3(
Mathf.Clamp(transform.position.x, -4, 4),
Mathf.Clamp(transform.position.y, -3, 3),
transform.position.z);
}
}
The general camera script (incl zooming):
var mover : Transform;
var target : Transform;
var distance = 10.0;
var MinDistance= 1.0;
var MaxDistance = 1.0;
var zoomRate = 1;
private var x = 0.0;
private var y = 0.0;
function Start ()
{
var angles = transform.eulerAngles;
x = angles.y;
y = angles.x;
}
function LateUpdate()
{
if (target)
var rotation = Quaternion.Euler(y, x, 0);
var position = rotation * Vector3(0.0, 0.0, -distance) + target.position;
transform.rotation = rotation;
transform.position = position + mover.position;
distance += -Input.GetAxis("Mouse ScrollWheel") * zoomRate * Mathf.Abs(distance);
distance = Mathf.Clamp(distance, MinDistance, MaxDistance);
For object rotation (bound to the gameobject):
#pragma strict
var xSpeed = 10;
var ySpeed = 10;
private var x = 0.0;
private var y = 0.0;
function Start ()
{
var angles = transform.eulerAngles;
x = angles.y;
y = angles.x;
// Make the rigid body not change rotation
if (rigidbody)
rigidbody.freezeRotation = true;
}
function Update () {
if (Input.GetMouseButton(0))
{
x += Input.GetAxis("Mouse X") * xSpeed * 0.02;
y -= Input.GetAxis("Mouse Y") * ySpeed * 0.02;
}
var rotation = Quaternion.Euler(y, x, 0);
transform.rotation = rotation;
}
I’m too unexperienced in Unity to figure out how to solve this “edge limitation”, and searching the internet gives me so many different possible solutions, varying from camera.screentoworldpoint / camera.worldtoviewpoint / using camera frustums / clamping, etc, so Id like to give it a try here (clamping isnt what we are looking for sadly)
Apologies if this is something very easy to do, but thanks in advance!