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

You are not connected. Please login or register

Unity Ninja Unity 3D Forum » General Dicussions » smooth camera transition between vector3's with stopping in between

smooth camera transition between vector3's with stopping in between

View previous topic View next topic Go down  Message [Page 1 of 1]

Makarios


Member [Lv. 1]
Member [Lv. 1]
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.

View user profile

SpaceKryptonite


Member [Lv. 3]
Member [Lv. 3]
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) {
TargetNum++;
if (TargetNum > Locations.Length-1) {
TargetNum = Locations.Length-1;
}
ChangeTarget();
}
if (Input.GetAxis("Mouse ScrollWheel") < 0) {
TargetNum--;
if (TargetNum < 0) {
TargetNum = 0;
}
ChangeTarget();
}
// 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];
}

View user profile http://vgbam.com/

View previous topic View next topic Back to top  Message [Page 1 of 1]

Permissions in this forum:
You cannot reply to topics in this forum