- Home /
Changing The Camera Position & Offsetting LookAt
Basically the illustration demonstrates what I'm trying to achieve, It may not even be a case of changing the LookAt Target, but more changing the camera's rotation to face the "top" on my GameObject, modifying the MouseOrbit.js.
In short: On zooming in using the mouse scrollwheel I want the camera to slightly rotate to focus on the top of the object, but at the same time be able to freely rotation around the same LookAt Target, Just like Unity's in built MouseOrbit.js
var target : Transform; //distance vars var distance = 40.0; var minDistance = 3.0; var maxDistance = 40.0; var zoomDistance = 3.0;
function LateUpdate(){
var lookAt = target.position;
var lookAtTop = target.position + Vector3.up;
var position = Vector3(0.0, 0.0, -distance) + lookAt;
transform.position = position;
if (camera) {
distance -= Input.GetAxis("Mouse ScrollWheel") * Time.deltaTime * Mathf.Abs(distance);
distance = Mathf.Clamp(distance, minDistance, maxDistance);
}
}
voted up for neat little illustration. I think you could change the lookat target to an empty game object which is set to the top of your current lookat target, or there's probably a way to script an offset of the current lookat target. Havn't answered because your scripts a bit to hectic for me to take a crack right now but if my comment is not clear and your still in need of help later comment back and I'l try demonstrate/script an example of what I mean :)
Thanks for the feedback and the vote up :D I was in two $$anonymous$$ds about pasting the whole script up! I really hate seeing other peoples jumble of script, but I felt I needed a direct modification of my own revision of the $$anonymous$$ouseLook.js. An example would be great. Thanks a lot for your time!
still working on this one it's trickier than I first expected. in the mean time take a look at trying to offset your lookat target on the y value
http://unity3d.com/support/documentation/ScriptReference/Transform.LookAt.html
http://unity3d.com/support/documentation/ScriptReference/Quaternion.LookRotation.html
var targetPos = target; var offset: Vector3 = transform.position - targetPos; transform.LookAt(transform.position + offset);
Offsets the LookAt backwards, that's how far I've got.
Answer by Scribe · Mar 19, 2011 at 01:33 PM
var maxview = 60; //the maximum field of view of the camera var minview = 10; //the minimum field of view of the camera var target1 : Transform; //the shape your looking at var maxOffset = 1; //the maximum offset to look at the top of the shape var minOffset = 0; //the normal Y offset var Offset = 0.00; //the offset of the camera 2 decimal places for smooth curve
function Update () {
if (Input.GetAxis("Mouse ScrollWheel") < 0) { //if scroll is less than 0
Camera.main.fieldOfView += 1; //add field of view (increase sight
Offset -= 0.02; //number = maxOffset/(maxview - minview);
if(Camera.main.fieldOfView >= maxview){
Camera.main.fieldOfView = maxview; //clamp to max field of view
}
if(Offset <= minOffset){
Offset = minOffset; //decrease offset until min offset
}
}
if (Input.GetAxis("Mouse ScrollWheel") > 0) { //if scroll is more than 0
Camera.main.fieldOfView -= 1; //decrease field of view (zoom)
Offset += 0.02; //number = maxOffset/(maxview - minview);
if(Camera.main.fieldOfView <= minview){
Camera.main.fieldOfView = minview;
}
if(Offset >= maxOffset){
Offset = maxOffset;
}
}
var target2 = target1.position + Vector3(0, Offset, 0);
transform.LookAt(target2); //look at target + offset
}
hope this helps put this on your camera
Scribe
Answer by Statement · Mar 16, 2011 at 03:08 PM
Apologies for not reading through all of your code. Can you not just lerp the look-at target and camera height as you come closer to the object? Alternatively you can set a position the object should attempt to move toward if you want a smoother movement.
var lookAt = target.position; var lookAtTop = target.position + Vector3.up; // change this to whatever you like
var factor = Mathf.Clamp01(distance / zoomDistance); lookAt = Vector3.Lerp(lookAtTop, lookAt, factor); position.y = Mathf.Lerp(lookAtTop.y, lookAt.y, factor) / 2.0f;
transform.LookAt(lookAt);
Thanks for the reply Statement I will give this solution a try later tonight and get back to you.
this has thrown me "The best overload for the method 'UnityEngine.$$anonymous$$athf.Lerp(float, float, float)' is not compatible with the argument list '(UnityEngine.Vector3, UnityEngine.Vector3, float)'." although the scripting reference clearly states (UnityEngine.Vector3, UnityEngine.Vector3, float)
Oh right, it should be the y components of the vectors in the code. Let me update quickly.
I am using $$anonymous$$athf.Lerp for the Y component, and was using vectors as input when I wanted floats.
Sorry statement i'm a bit confused, I've edited my main post to show you what I'm using so far. I can't quite grasp how I can implement the mathf.lerp