Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
1
Question by Flavor · Nov 13, 2015 at 12:23 AM · c#lerpvector2

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;
             }
         }
     }
 
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
3
Best Answer

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;
         }
     }
 }
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

Answer by Flavor · Nov 13, 2015 at 12:49 PM

@OncaLupe

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!

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

34 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges