FragMate CommunityLog in

FragMate - Software and Game Developer Community, including Resources, Forums, Marketplace & More.


descriptionsmooth camera transition between vector3's with stopping in between

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.

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

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];

I am Space! Bring Cookies, and I will bring Joy! Razz
Permissions in this forum:
You cannot reply to topics in this forum