Unity Ninja Unity 3D Forum
To have exclusive access to all downloads, recourses and links within this forum, a free registered account is required. Just a heads up Wink
Go down
Member [Lv. 1]
Member [Lv. 1]
Posts : 2
Points : 1352
Rep : 0
Join date : 2014-12-09
View user profile

smooth camera transition between vector3's with stopping in between

on Sun Apr 12, 2015 9:22 am
in the project I'm working on I want camera zoom to go from a high point far above the character allowing near overhead wide range view showing an area of several buildings around the player, to a low point that is just above and behind the player zoomed to show roughly the room the player is in and from a much more angled (horizontal) look.

the max zoom out location is 0,45,-20 from the player, the max zoom in is 0,5.5,-6 from the player

as far as changing the angle to make sure the player is still center camera i can just update the .lookat every time the camera moves, but I want to know the best way to smoothly change between the points using the scroll wheel, so that i can stop anywhere in between.
Member [Lv. 3]
Member [Lv. 3]
Posts : 16
Points : 1278
Rep : 0
Join date : 2015-04-13
Age : 26
Location : Milwaukee, WI
View user profilehttp://vgbam.com/

Re: smooth camera transition between vector3's with stopping in between

on Mon Apr 13, 2015 4:17 pm
Here is what I came up with. Add transforms in the Locations Array in the inspector. You can also change Speed if you want. TargetNum is to show you what location it is going to. Let me know if this helps. Add this script to the camera.

private Transform currentposition;
private Transform targetposition;
public Transform [] Locations = new Transform[2];
public float speed = 1.0F;
public int TargetNum = 0;
private int PreviousTargetNum = 0;
private float startTime;
private float journeyLength;

void Awake() {
// Set A as first target
startTime = Time.time;
currentposition = transform;
targetposition = Locations[0];
journeyLength = Vector3.Distance(currentposition.position, Locations[0].position);
void Update() {
// Get Mouse Wheel Inputs
if (Input.GetAxis("Mouse ScrollWheel") > 0) {
if (TargetNum > Locations.Length-1) {
TargetNum = Locations.Length-1;
if (Input.GetAxis("Mouse ScrollWheel") < 0) {
if (TargetNum < 0) {
TargetNum = 0;
// Perform Smooth move to target
float distCovered = (Time.time - startTime) * speed;
float fracJourney = distCovered / journeyLength;
transform.position = Vector3.Lerp(currentposition.position, targetposition.position, fracJourney);

// Trigger for changing targets
void ChangeTarget() {
// Prevent jitter when mouse wheeling back and forth
if (PreviousTargetNum != TargetNum) {
// reset transition to new target
startTime = Time.time;
currentposition = transform;
PreviousTargetNum = TargetNum;
journeyLength = Vector3.Distance(currentposition.position, Locations[TargetNum].position);
// Change target base off TargetNum
targetposition = Locations[TargetNum];
Back to top
Permissions in this forum:
You cannot reply to topics in this forum