Help with Vector2.Lerp
Hi!
Disclaimer! I'm far from a C# pro and I'm trying to piece this from what I can find on here & goolge... :)
Basically what I'm trying to achieve is "While mouse over gameObject, if mouse click, move the main camera center (X,Y) to the center (X,Y) of the clicked gameObject"...
It does move but to the wrong vectors, it changes the Z eventhough its a Vector2 and it teleports instead of lerping... ??
Help? Code looks like this...
using UnityEngine;
using System.Collections;
public class Controller : MonoBehaviour {
private float startTime;
private Vector2 startMarker;
private Vector2 endMarker;
private bool moveTrue;
void Awake(){
moveTrue = false;
}
// Use this for initialization
void Start() {
}
// Update is called once per frame
void Update () {
if (startMarker == endMarker){
moveTrue = false;
}
if (moveTrue == true) {
Camera.main.transform.position = Vector2.Lerp (startMarker, endMarker, (startTime + Time.deltaTime) / 5.0f);
}
}
void OnMouseOver() {
if (Input.GetMouseButtonDown (0)) {
startMarker = Camera.main.transform.position;
endMarker = gameObject.transform.position;
moveTrue = true;
startTime = -Time.time;
}
}
}
Answer by OncaLupe · Nov 13, 2015 at 03:01 AM
First, all movements and positions in Unity use a Vector3. If you try to use a Vec2, Unity will convert it to a Vec3 with Z at 0.
Second, the first if() in the Update() method isn't doing anything. Once you click on the object, start and end markers will never be the same. You should check distance between camera and endMarker inside the second if().
Finally, the usage of Lerp (besides being a Vec2) is not set up properly. This is understandable though, as it can be confusing. The final argument should end up a float between 0 and 1 that says where in the range between start and end it should be. When using to smooth movement like this, you take the current time and subtract the start time to get how far into the movement it is, then divide by the time it should take.
I adjusted your script to take all these changes in:
using UnityEngine;
using System.Collections;
public class Controller : MonoBehaviour {
private float startTime;
private Vector3 startMarker;
private Vector3 endMarker;
private bool moveTrue = false;
private float timeToMoveCamera = 5f;//Seconds it takes to move the camera
void Update () {
if (moveTrue) {
Camera.main.transform.position = Vector3.Lerp (startMarker, endMarker, (Time.time - startTime) / timeToMoveCamera);
//If at the endMarker (kEpsilon is a very tiny number, trying to test equality on Vectors/floats can sometimes have issues)
if(Vector3.Distance(Camera.main.transform.position, endMarker) < Vector3.kEpsilon) {
moveTrue = false;
}
}
}
void OnMouseOver() {
if (Input.GetMouseButtonDown (0)) {
startMarker = Camera.main.transform.position;
endMarker = gameObject.transform.position;
endMarker.z = startMarker.z;//Keep the Z position of the camera where it was
moveTrue = true;
startTime = Time.time;
}
}
}
Answer by Flavor · Nov 13, 2015 at 12:49 PM
Thanks alot, just integrated it to my script and it works flawlessly!!
I could have wandered in the dark a little while still until I would've have gone back to vec3 to bullet proof that Z :S... Extra kudos for the quick response!
Cheers!
Your answer
Follow this Question
Related Questions
Lerping at a constant rate? 2 Answers
Problems with Trigonometry and LERPing 0 Answers
Color.lerp for Spriterender is not smooth 1 Answer
hi everyone how can i move an object from a point to specific point(point b) by mouse click 0 Answers
[C#] Issue with calculating transform.position and Vector2D 1 Answer