Move or lerp slowly game object to other game object's position by single click on UI Button not on multiple clicks? [duplicate]










0
















This question already has an answer here:



  • Move GameObject over time

    3 answers



I have a button in my scene, a main camera and an empty game object.



Whenever I click on that button my camera should slowly lerp to that empty game object's position.



My camera is lerping to that position, which is fine, but the problem is that I have to click multiple times on that button until my camera reach its position. So is there a way to move my camera to game object's position using a single click on that button?



Here is my code:



using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class TransitionCAM : MonoBehaviour
public Transform views;
public float transitionSPEED;
Transform currentVIEW;

public void move()
currentVIEW = views;
transform.position = Vector3.Lerp (transform.position, currentVIEW.position, Time.deltaTime * transitionSPEED);

//for camera rotation
Vector3 currentangel = new Vector3 ( Mathf.LerpAngle(transform.rotation.eulerAngles.x, currentVIEW.transform.rotation.eulerAngles.x, Time.deltaTime *transitionSPEED),
Mathf.LerpAngle(transform.rotation.eulerAngles.y, currentVIEW.transform.rotation.eulerAngles.y, Time.deltaTime *transitionSPEED),
Mathf.LerpAngle(transform.rotation.eulerAngles.z, currentVIEW.transform.rotation.eulerAngles.z, Time.deltaTime *transitionSPEED));

transform.eulerAngles = currentangel;











share|improve this question















marked as duplicate by derHugo, Programmer c#
Users with the  c# badge can single-handedly close c# questions as duplicates and reopen them as needed.

StackExchange.ready(function()
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();

);
);
);
Nov 14 '18 at 15:41


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.


















  • can you add your code the question, please

    – derHugo
    Nov 14 '18 at 12:57











  • public Transform views; public float transitionSPEED; Transform currentVIEW; public void move(){ currentVIEW = views; transform.position = Vector3.Lerp (transform.position, currentVIEW.position, Time.deltaTime * transitionSPEED);

    – Nouman Khan
    Nov 14 '18 at 13:06












  • @derHugo check my question now my code is successfuly lerping the camera but on multiple clicks on ui button i want this lerping to be happen on a single click on ui button

    – Nouman Khan
    Nov 14 '18 at 13:14











  • How do you call this move function probably the problem is there

    – Ali Kanat
    Nov 14 '18 at 13:19











  • @alikanat i attach this script to my main camera then i drag my main camera into the on click event of button and from there i call this function

    – Nouman Khan
    Nov 14 '18 at 13:24















0
















This question already has an answer here:



  • Move GameObject over time

    3 answers



I have a button in my scene, a main camera and an empty game object.



Whenever I click on that button my camera should slowly lerp to that empty game object's position.



My camera is lerping to that position, which is fine, but the problem is that I have to click multiple times on that button until my camera reach its position. So is there a way to move my camera to game object's position using a single click on that button?



Here is my code:



using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class TransitionCAM : MonoBehaviour
public Transform views;
public float transitionSPEED;
Transform currentVIEW;

public void move()
currentVIEW = views;
transform.position = Vector3.Lerp (transform.position, currentVIEW.position, Time.deltaTime * transitionSPEED);

//for camera rotation
Vector3 currentangel = new Vector3 ( Mathf.LerpAngle(transform.rotation.eulerAngles.x, currentVIEW.transform.rotation.eulerAngles.x, Time.deltaTime *transitionSPEED),
Mathf.LerpAngle(transform.rotation.eulerAngles.y, currentVIEW.transform.rotation.eulerAngles.y, Time.deltaTime *transitionSPEED),
Mathf.LerpAngle(transform.rotation.eulerAngles.z, currentVIEW.transform.rotation.eulerAngles.z, Time.deltaTime *transitionSPEED));

transform.eulerAngles = currentangel;











share|improve this question















marked as duplicate by derHugo, Programmer c#
Users with the  c# badge can single-handedly close c# questions as duplicates and reopen them as needed.

StackExchange.ready(function()
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();

);
);
);
Nov 14 '18 at 15:41


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.


















  • can you add your code the question, please

    – derHugo
    Nov 14 '18 at 12:57











  • public Transform views; public float transitionSPEED; Transform currentVIEW; public void move(){ currentVIEW = views; transform.position = Vector3.Lerp (transform.position, currentVIEW.position, Time.deltaTime * transitionSPEED);

    – Nouman Khan
    Nov 14 '18 at 13:06












  • @derHugo check my question now my code is successfuly lerping the camera but on multiple clicks on ui button i want this lerping to be happen on a single click on ui button

    – Nouman Khan
    Nov 14 '18 at 13:14











  • How do you call this move function probably the problem is there

    – Ali Kanat
    Nov 14 '18 at 13:19











  • @alikanat i attach this script to my main camera then i drag my main camera into the on click event of button and from there i call this function

    – Nouman Khan
    Nov 14 '18 at 13:24













0












0








0









This question already has an answer here:



  • Move GameObject over time

    3 answers



I have a button in my scene, a main camera and an empty game object.



Whenever I click on that button my camera should slowly lerp to that empty game object's position.



My camera is lerping to that position, which is fine, but the problem is that I have to click multiple times on that button until my camera reach its position. So is there a way to move my camera to game object's position using a single click on that button?



Here is my code:



using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class TransitionCAM : MonoBehaviour
public Transform views;
public float transitionSPEED;
Transform currentVIEW;

public void move()
currentVIEW = views;
transform.position = Vector3.Lerp (transform.position, currentVIEW.position, Time.deltaTime * transitionSPEED);

//for camera rotation
Vector3 currentangel = new Vector3 ( Mathf.LerpAngle(transform.rotation.eulerAngles.x, currentVIEW.transform.rotation.eulerAngles.x, Time.deltaTime *transitionSPEED),
Mathf.LerpAngle(transform.rotation.eulerAngles.y, currentVIEW.transform.rotation.eulerAngles.y, Time.deltaTime *transitionSPEED),
Mathf.LerpAngle(transform.rotation.eulerAngles.z, currentVIEW.transform.rotation.eulerAngles.z, Time.deltaTime *transitionSPEED));

transform.eulerAngles = currentangel;











share|improve this question

















This question already has an answer here:



  • Move GameObject over time

    3 answers



I have a button in my scene, a main camera and an empty game object.



Whenever I click on that button my camera should slowly lerp to that empty game object's position.



My camera is lerping to that position, which is fine, but the problem is that I have to click multiple times on that button until my camera reach its position. So is there a way to move my camera to game object's position using a single click on that button?



Here is my code:



using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class TransitionCAM : MonoBehaviour
public Transform views;
public float transitionSPEED;
Transform currentVIEW;

public void move()
currentVIEW = views;
transform.position = Vector3.Lerp (transform.position, currentVIEW.position, Time.deltaTime * transitionSPEED);

//for camera rotation
Vector3 currentangel = new Vector3 ( Mathf.LerpAngle(transform.rotation.eulerAngles.x, currentVIEW.transform.rotation.eulerAngles.x, Time.deltaTime *transitionSPEED),
Mathf.LerpAngle(transform.rotation.eulerAngles.y, currentVIEW.transform.rotation.eulerAngles.y, Time.deltaTime *transitionSPEED),
Mathf.LerpAngle(transform.rotation.eulerAngles.z, currentVIEW.transform.rotation.eulerAngles.z, Time.deltaTime *transitionSPEED));

transform.eulerAngles = currentangel;






This question already has an answer here:



  • Move GameObject over time

    3 answers







c# unity3d






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 14 '18 at 21:23









pushkin

4,030112752




4,030112752










asked Nov 14 '18 at 12:44









Nouman KhanNouman Khan

116




116




marked as duplicate by derHugo, Programmer c#
Users with the  c# badge can single-handedly close c# questions as duplicates and reopen them as needed.

StackExchange.ready(function()
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();

);
);
);
Nov 14 '18 at 15:41


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.









marked as duplicate by derHugo, Programmer c#
Users with the  c# badge can single-handedly close c# questions as duplicates and reopen them as needed.

StackExchange.ready(function()
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();

);
);
);
Nov 14 '18 at 15:41


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.














  • can you add your code the question, please

    – derHugo
    Nov 14 '18 at 12:57











  • public Transform views; public float transitionSPEED; Transform currentVIEW; public void move(){ currentVIEW = views; transform.position = Vector3.Lerp (transform.position, currentVIEW.position, Time.deltaTime * transitionSPEED);

    – Nouman Khan
    Nov 14 '18 at 13:06












  • @derHugo check my question now my code is successfuly lerping the camera but on multiple clicks on ui button i want this lerping to be happen on a single click on ui button

    – Nouman Khan
    Nov 14 '18 at 13:14











  • How do you call this move function probably the problem is there

    – Ali Kanat
    Nov 14 '18 at 13:19











  • @alikanat i attach this script to my main camera then i drag my main camera into the on click event of button and from there i call this function

    – Nouman Khan
    Nov 14 '18 at 13:24

















  • can you add your code the question, please

    – derHugo
    Nov 14 '18 at 12:57











  • public Transform views; public float transitionSPEED; Transform currentVIEW; public void move(){ currentVIEW = views; transform.position = Vector3.Lerp (transform.position, currentVIEW.position, Time.deltaTime * transitionSPEED);

    – Nouman Khan
    Nov 14 '18 at 13:06












  • @derHugo check my question now my code is successfuly lerping the camera but on multiple clicks on ui button i want this lerping to be happen on a single click on ui button

    – Nouman Khan
    Nov 14 '18 at 13:14











  • How do you call this move function probably the problem is there

    – Ali Kanat
    Nov 14 '18 at 13:19











  • @alikanat i attach this script to my main camera then i drag my main camera into the on click event of button and from there i call this function

    – Nouman Khan
    Nov 14 '18 at 13:24
















can you add your code the question, please

– derHugo
Nov 14 '18 at 12:57





can you add your code the question, please

– derHugo
Nov 14 '18 at 12:57













public Transform views; public float transitionSPEED; Transform currentVIEW; public void move(){ currentVIEW = views; transform.position = Vector3.Lerp (transform.position, currentVIEW.position, Time.deltaTime * transitionSPEED);

– Nouman Khan
Nov 14 '18 at 13:06






public Transform views; public float transitionSPEED; Transform currentVIEW; public void move(){ currentVIEW = views; transform.position = Vector3.Lerp (transform.position, currentVIEW.position, Time.deltaTime * transitionSPEED);

– Nouman Khan
Nov 14 '18 at 13:06














@derHugo check my question now my code is successfuly lerping the camera but on multiple clicks on ui button i want this lerping to be happen on a single click on ui button

– Nouman Khan
Nov 14 '18 at 13:14





@derHugo check my question now my code is successfuly lerping the camera but on multiple clicks on ui button i want this lerping to be happen on a single click on ui button

– Nouman Khan
Nov 14 '18 at 13:14













How do you call this move function probably the problem is there

– Ali Kanat
Nov 14 '18 at 13:19





How do you call this move function probably the problem is there

– Ali Kanat
Nov 14 '18 at 13:19













@alikanat i attach this script to my main camera then i drag my main camera into the on click event of button and from there i call this function

– Nouman Khan
Nov 14 '18 at 13:24





@alikanat i attach this script to my main camera then i drag my main camera into the on click event of button and from there i call this function

– Nouman Khan
Nov 14 '18 at 13:24












1 Answer
1






active

oldest

votes


















0














Problem is your Vector3.Lerp runs only once. Try this instead:



 public Transform views;
public float transitionSPEED;
Transform currentVIEW;
private bool flag = false;

Vector3 currentangel;
private void Start()

Debug.Log(flag);
currentVIEW = views;


private void Update()

if (flag == true)


transform.position = Vector3.Lerp(transform.position, currentVIEW.position, Time.deltaTime * transitionSPEED);
Debug.Log("object pos" + transform.position);
Debug.Log(currentVIEW.position);
//for camera rotation

currentangel = new Vector3(Mathf.LerpAngle(transform.rotation.eulerAngles.x, currentVIEW.transform.rotation.eulerAngles.x, Time.deltaTime * transitionSPEED),
Mathf.LerpAngle(transform.rotation.eulerAngles.y, currentVIEW.transform.rotation.eulerAngles.y, Time.deltaTime * transitionSPEED),
Mathf.LerpAngle(transform.rotation.eulerAngles.z, currentVIEW.transform.rotation.eulerAngles.z, Time.deltaTime * transitionSPEED));

transform.eulerAngles = currentangel;





public void Move()

flag = true;



You have to come up with a way to stop lerping when your camera is at correct position. Because this if (flag == true) will be always true after button is pressed.



This is a better solution i believe using Coroutine



public Transform views;
public float transitionSPEED;
Transform currentVIEW;
private bool flag = false;
private bool isStarted = false;
Vector3 currentangel;
private void Start()

Debug.Log(flag);
currentVIEW = views;


private void Update()

if (flag && !isStarted)

StartCoroutine(CoroutineSolution());
isStarted = true;


IEnumerator CoroutineSolution()

float t = 0.0f;
while ( t<1.0f )


t += Time.deltaTime;
transform.position = Vector3.Lerp(transform.position, currentVIEW.position, t);

//for camera rotation

currentangel = new Vector3(Mathf.LerpAngle(transform.rotation.eulerAngles.x, currentVIEW.transform.rotation.eulerAngles.x, t),
Mathf.LerpAngle(transform.rotation.eulerAngles.y, currentVIEW.transform.rotation.eulerAngles.y, t),
Mathf.LerpAngle(transform.rotation.eulerAngles.z, currentVIEW.transform.rotation.eulerAngles.z, t));

transform.eulerAngles = currentangel;
Debug.Log("Coroutine running");

yield return null;






public void Move()

flag = true;






share|improve this answer

























  • thanks man this worked for me

    – Nouman Khan
    Nov 14 '18 at 14:12











  • your this Coroutine Solutions works fine if i have only 1 button and move camera to only single objects position i have multiple buttons and multiple objects when i press button 1 it moves from one position to another but when i press button 2 it doesnot move

    – Nouman Khan
    Nov 27 '18 at 14:08











  • Well you have to modify it to make it work for multiple buttons

    – Ali Kanat
    Nov 27 '18 at 15:53











  • please modify it for me please i will be very thankful to u

    – Nouman Khan
    Nov 27 '18 at 16:06











  • i manage to move my camera to all objects position but whenever my camera reaches at third game object position my camera starts to move in another direction with slow speed what i did is put isStarted = false in my all below public functions

    – Nouman Khan
    Nov 27 '18 at 17:21

















1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














Problem is your Vector3.Lerp runs only once. Try this instead:



 public Transform views;
public float transitionSPEED;
Transform currentVIEW;
private bool flag = false;

Vector3 currentangel;
private void Start()

Debug.Log(flag);
currentVIEW = views;


private void Update()

if (flag == true)


transform.position = Vector3.Lerp(transform.position, currentVIEW.position, Time.deltaTime * transitionSPEED);
Debug.Log("object pos" + transform.position);
Debug.Log(currentVIEW.position);
//for camera rotation

currentangel = new Vector3(Mathf.LerpAngle(transform.rotation.eulerAngles.x, currentVIEW.transform.rotation.eulerAngles.x, Time.deltaTime * transitionSPEED),
Mathf.LerpAngle(transform.rotation.eulerAngles.y, currentVIEW.transform.rotation.eulerAngles.y, Time.deltaTime * transitionSPEED),
Mathf.LerpAngle(transform.rotation.eulerAngles.z, currentVIEW.transform.rotation.eulerAngles.z, Time.deltaTime * transitionSPEED));

transform.eulerAngles = currentangel;





public void Move()

flag = true;



You have to come up with a way to stop lerping when your camera is at correct position. Because this if (flag == true) will be always true after button is pressed.



This is a better solution i believe using Coroutine



public Transform views;
public float transitionSPEED;
Transform currentVIEW;
private bool flag = false;
private bool isStarted = false;
Vector3 currentangel;
private void Start()

Debug.Log(flag);
currentVIEW = views;


private void Update()

if (flag && !isStarted)

StartCoroutine(CoroutineSolution());
isStarted = true;


IEnumerator CoroutineSolution()

float t = 0.0f;
while ( t<1.0f )


t += Time.deltaTime;
transform.position = Vector3.Lerp(transform.position, currentVIEW.position, t);

//for camera rotation

currentangel = new Vector3(Mathf.LerpAngle(transform.rotation.eulerAngles.x, currentVIEW.transform.rotation.eulerAngles.x, t),
Mathf.LerpAngle(transform.rotation.eulerAngles.y, currentVIEW.transform.rotation.eulerAngles.y, t),
Mathf.LerpAngle(transform.rotation.eulerAngles.z, currentVIEW.transform.rotation.eulerAngles.z, t));

transform.eulerAngles = currentangel;
Debug.Log("Coroutine running");

yield return null;






public void Move()

flag = true;






share|improve this answer

























  • thanks man this worked for me

    – Nouman Khan
    Nov 14 '18 at 14:12











  • your this Coroutine Solutions works fine if i have only 1 button and move camera to only single objects position i have multiple buttons and multiple objects when i press button 1 it moves from one position to another but when i press button 2 it doesnot move

    – Nouman Khan
    Nov 27 '18 at 14:08











  • Well you have to modify it to make it work for multiple buttons

    – Ali Kanat
    Nov 27 '18 at 15:53











  • please modify it for me please i will be very thankful to u

    – Nouman Khan
    Nov 27 '18 at 16:06











  • i manage to move my camera to all objects position but whenever my camera reaches at third game object position my camera starts to move in another direction with slow speed what i did is put isStarted = false in my all below public functions

    – Nouman Khan
    Nov 27 '18 at 17:21















0














Problem is your Vector3.Lerp runs only once. Try this instead:



 public Transform views;
public float transitionSPEED;
Transform currentVIEW;
private bool flag = false;

Vector3 currentangel;
private void Start()

Debug.Log(flag);
currentVIEW = views;


private void Update()

if (flag == true)


transform.position = Vector3.Lerp(transform.position, currentVIEW.position, Time.deltaTime * transitionSPEED);
Debug.Log("object pos" + transform.position);
Debug.Log(currentVIEW.position);
//for camera rotation

currentangel = new Vector3(Mathf.LerpAngle(transform.rotation.eulerAngles.x, currentVIEW.transform.rotation.eulerAngles.x, Time.deltaTime * transitionSPEED),
Mathf.LerpAngle(transform.rotation.eulerAngles.y, currentVIEW.transform.rotation.eulerAngles.y, Time.deltaTime * transitionSPEED),
Mathf.LerpAngle(transform.rotation.eulerAngles.z, currentVIEW.transform.rotation.eulerAngles.z, Time.deltaTime * transitionSPEED));

transform.eulerAngles = currentangel;





public void Move()

flag = true;



You have to come up with a way to stop lerping when your camera is at correct position. Because this if (flag == true) will be always true after button is pressed.



This is a better solution i believe using Coroutine



public Transform views;
public float transitionSPEED;
Transform currentVIEW;
private bool flag = false;
private bool isStarted = false;
Vector3 currentangel;
private void Start()

Debug.Log(flag);
currentVIEW = views;


private void Update()

if (flag && !isStarted)

StartCoroutine(CoroutineSolution());
isStarted = true;


IEnumerator CoroutineSolution()

float t = 0.0f;
while ( t<1.0f )


t += Time.deltaTime;
transform.position = Vector3.Lerp(transform.position, currentVIEW.position, t);

//for camera rotation

currentangel = new Vector3(Mathf.LerpAngle(transform.rotation.eulerAngles.x, currentVIEW.transform.rotation.eulerAngles.x, t),
Mathf.LerpAngle(transform.rotation.eulerAngles.y, currentVIEW.transform.rotation.eulerAngles.y, t),
Mathf.LerpAngle(transform.rotation.eulerAngles.z, currentVIEW.transform.rotation.eulerAngles.z, t));

transform.eulerAngles = currentangel;
Debug.Log("Coroutine running");

yield return null;






public void Move()

flag = true;






share|improve this answer

























  • thanks man this worked for me

    – Nouman Khan
    Nov 14 '18 at 14:12











  • your this Coroutine Solutions works fine if i have only 1 button and move camera to only single objects position i have multiple buttons and multiple objects when i press button 1 it moves from one position to another but when i press button 2 it doesnot move

    – Nouman Khan
    Nov 27 '18 at 14:08











  • Well you have to modify it to make it work for multiple buttons

    – Ali Kanat
    Nov 27 '18 at 15:53











  • please modify it for me please i will be very thankful to u

    – Nouman Khan
    Nov 27 '18 at 16:06











  • i manage to move my camera to all objects position but whenever my camera reaches at third game object position my camera starts to move in another direction with slow speed what i did is put isStarted = false in my all below public functions

    – Nouman Khan
    Nov 27 '18 at 17:21













0












0








0







Problem is your Vector3.Lerp runs only once. Try this instead:



 public Transform views;
public float transitionSPEED;
Transform currentVIEW;
private bool flag = false;

Vector3 currentangel;
private void Start()

Debug.Log(flag);
currentVIEW = views;


private void Update()

if (flag == true)


transform.position = Vector3.Lerp(transform.position, currentVIEW.position, Time.deltaTime * transitionSPEED);
Debug.Log("object pos" + transform.position);
Debug.Log(currentVIEW.position);
//for camera rotation

currentangel = new Vector3(Mathf.LerpAngle(transform.rotation.eulerAngles.x, currentVIEW.transform.rotation.eulerAngles.x, Time.deltaTime * transitionSPEED),
Mathf.LerpAngle(transform.rotation.eulerAngles.y, currentVIEW.transform.rotation.eulerAngles.y, Time.deltaTime * transitionSPEED),
Mathf.LerpAngle(transform.rotation.eulerAngles.z, currentVIEW.transform.rotation.eulerAngles.z, Time.deltaTime * transitionSPEED));

transform.eulerAngles = currentangel;





public void Move()

flag = true;



You have to come up with a way to stop lerping when your camera is at correct position. Because this if (flag == true) will be always true after button is pressed.



This is a better solution i believe using Coroutine



public Transform views;
public float transitionSPEED;
Transform currentVIEW;
private bool flag = false;
private bool isStarted = false;
Vector3 currentangel;
private void Start()

Debug.Log(flag);
currentVIEW = views;


private void Update()

if (flag && !isStarted)

StartCoroutine(CoroutineSolution());
isStarted = true;


IEnumerator CoroutineSolution()

float t = 0.0f;
while ( t<1.0f )


t += Time.deltaTime;
transform.position = Vector3.Lerp(transform.position, currentVIEW.position, t);

//for camera rotation

currentangel = new Vector3(Mathf.LerpAngle(transform.rotation.eulerAngles.x, currentVIEW.transform.rotation.eulerAngles.x, t),
Mathf.LerpAngle(transform.rotation.eulerAngles.y, currentVIEW.transform.rotation.eulerAngles.y, t),
Mathf.LerpAngle(transform.rotation.eulerAngles.z, currentVIEW.transform.rotation.eulerAngles.z, t));

transform.eulerAngles = currentangel;
Debug.Log("Coroutine running");

yield return null;






public void Move()

flag = true;






share|improve this answer















Problem is your Vector3.Lerp runs only once. Try this instead:



 public Transform views;
public float transitionSPEED;
Transform currentVIEW;
private bool flag = false;

Vector3 currentangel;
private void Start()

Debug.Log(flag);
currentVIEW = views;


private void Update()

if (flag == true)


transform.position = Vector3.Lerp(transform.position, currentVIEW.position, Time.deltaTime * transitionSPEED);
Debug.Log("object pos" + transform.position);
Debug.Log(currentVIEW.position);
//for camera rotation

currentangel = new Vector3(Mathf.LerpAngle(transform.rotation.eulerAngles.x, currentVIEW.transform.rotation.eulerAngles.x, Time.deltaTime * transitionSPEED),
Mathf.LerpAngle(transform.rotation.eulerAngles.y, currentVIEW.transform.rotation.eulerAngles.y, Time.deltaTime * transitionSPEED),
Mathf.LerpAngle(transform.rotation.eulerAngles.z, currentVIEW.transform.rotation.eulerAngles.z, Time.deltaTime * transitionSPEED));

transform.eulerAngles = currentangel;





public void Move()

flag = true;



You have to come up with a way to stop lerping when your camera is at correct position. Because this if (flag == true) will be always true after button is pressed.



This is a better solution i believe using Coroutine



public Transform views;
public float transitionSPEED;
Transform currentVIEW;
private bool flag = false;
private bool isStarted = false;
Vector3 currentangel;
private void Start()

Debug.Log(flag);
currentVIEW = views;


private void Update()

if (flag && !isStarted)

StartCoroutine(CoroutineSolution());
isStarted = true;


IEnumerator CoroutineSolution()

float t = 0.0f;
while ( t<1.0f )


t += Time.deltaTime;
transform.position = Vector3.Lerp(transform.position, currentVIEW.position, t);

//for camera rotation

currentangel = new Vector3(Mathf.LerpAngle(transform.rotation.eulerAngles.x, currentVIEW.transform.rotation.eulerAngles.x, t),
Mathf.LerpAngle(transform.rotation.eulerAngles.y, currentVIEW.transform.rotation.eulerAngles.y, t),
Mathf.LerpAngle(transform.rotation.eulerAngles.z, currentVIEW.transform.rotation.eulerAngles.z, t));

transform.eulerAngles = currentangel;
Debug.Log("Coroutine running");

yield return null;






public void Move()

flag = true;







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 14 '18 at 14:31

























answered Nov 14 '18 at 13:58









Ali KanatAli Kanat

6411416




6411416












  • thanks man this worked for me

    – Nouman Khan
    Nov 14 '18 at 14:12











  • your this Coroutine Solutions works fine if i have only 1 button and move camera to only single objects position i have multiple buttons and multiple objects when i press button 1 it moves from one position to another but when i press button 2 it doesnot move

    – Nouman Khan
    Nov 27 '18 at 14:08











  • Well you have to modify it to make it work for multiple buttons

    – Ali Kanat
    Nov 27 '18 at 15:53











  • please modify it for me please i will be very thankful to u

    – Nouman Khan
    Nov 27 '18 at 16:06











  • i manage to move my camera to all objects position but whenever my camera reaches at third game object position my camera starts to move in another direction with slow speed what i did is put isStarted = false in my all below public functions

    – Nouman Khan
    Nov 27 '18 at 17:21

















  • thanks man this worked for me

    – Nouman Khan
    Nov 14 '18 at 14:12











  • your this Coroutine Solutions works fine if i have only 1 button and move camera to only single objects position i have multiple buttons and multiple objects when i press button 1 it moves from one position to another but when i press button 2 it doesnot move

    – Nouman Khan
    Nov 27 '18 at 14:08











  • Well you have to modify it to make it work for multiple buttons

    – Ali Kanat
    Nov 27 '18 at 15:53











  • please modify it for me please i will be very thankful to u

    – Nouman Khan
    Nov 27 '18 at 16:06











  • i manage to move my camera to all objects position but whenever my camera reaches at third game object position my camera starts to move in another direction with slow speed what i did is put isStarted = false in my all below public functions

    – Nouman Khan
    Nov 27 '18 at 17:21
















thanks man this worked for me

– Nouman Khan
Nov 14 '18 at 14:12





thanks man this worked for me

– Nouman Khan
Nov 14 '18 at 14:12













your this Coroutine Solutions works fine if i have only 1 button and move camera to only single objects position i have multiple buttons and multiple objects when i press button 1 it moves from one position to another but when i press button 2 it doesnot move

– Nouman Khan
Nov 27 '18 at 14:08





your this Coroutine Solutions works fine if i have only 1 button and move camera to only single objects position i have multiple buttons and multiple objects when i press button 1 it moves from one position to another but when i press button 2 it doesnot move

– Nouman Khan
Nov 27 '18 at 14:08













Well you have to modify it to make it work for multiple buttons

– Ali Kanat
Nov 27 '18 at 15:53





Well you have to modify it to make it work for multiple buttons

– Ali Kanat
Nov 27 '18 at 15:53













please modify it for me please i will be very thankful to u

– Nouman Khan
Nov 27 '18 at 16:06





please modify it for me please i will be very thankful to u

– Nouman Khan
Nov 27 '18 at 16:06













i manage to move my camera to all objects position but whenever my camera reaches at third game object position my camera starts to move in another direction with slow speed what i did is put isStarted = false in my all below public functions

– Nouman Khan
Nov 27 '18 at 17:21





i manage to move my camera to all objects position but whenever my camera reaches at third game object position my camera starts to move in another direction with slow speed what i did is put isStarted = false in my all below public functions

– Nouman Khan
Nov 27 '18 at 17:21



Popular posts from this blog

Top Tejano songwriter Luis Silva dead of heart attack at 64

ReactJS Fetched API data displays live - need Data displayed static

Evgeni Malkin