• 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 /
avatar image
0
Question by CB-TV · May 03, 2014 at 09:01 AM · gameobjectmovementpositionvector3.lerp

Vector3.Lerp not working?

I have a Vector3.Lerp in an Update function but it doesn't seem to move my object. This is my script:

 private var elfMale;
 
 public var smooth = 2;
 private var newPosition;
 
 function Awake ()
 {
     //newPosition = elfMale;
 }        
 
 function  Start ()
 {
     elfFemale = GameObject.Find ("FemaleElf").transform.position;
     elfMale = GameObject.Find ("MaleElf").transform.position;
 }    
 
 function Update ()
 {
     if (move)
     {        
         newPosition = new Vector3 (10, 10, 0);
     
         elfMale = Vector3.Lerp (elfMale, newPosition, Time.deltaTime * smooth);
     }
 }

I used the Unity Official Tutorials video and they put: newPosition = elfMale in the Awake function but I've commented it out since it seems pointless for me. Any ideas of why this isn't working? Also, I've activated the move variable through a GUI button.

Comment
Add comment · Show 9
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 Subhajit-Nath · May 03, 2014 at 09:50 AM 0
Share

$$anonymous$$y first hunch is that the variable can't find the $$anonymous$$eleElf GameObject. First check that the gameobject isn't returning null.

avatar image CB-TV · May 03, 2014 at 09:55 AM 0
Share

Ok, I'll check.

avatar image Professor Snake · May 03, 2014 at 10:10 AM 1
Share

I'm pretty sure getting the elf$$anonymous$$ale.transform.position would just copy its values into the variable ins$$anonymous$$d of having the variable actually point to the position. This is something that doesn't happen with Transform, though, so try:

 elf$$anonymous$$ale = GameObject.Find ("$$anonymous$$aleElf").transform;

 elf$$anonymous$$ale.position = Vector3.Lerp (elf$$anonymous$$ale.position, newPosition, Time.deltaTime * smooth);
avatar image CB-TV · May 03, 2014 at 10:14 AM 0
Share

Ok, I'll try!

avatar image CB-TV · May 03, 2014 at 10:16 AM 0
Share

I think I actually tried this previously and it gave me the same error as I just got now:

"'position' is not a member of 'Object'."

Show more comments

5 Replies

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

Answer by MikeNewall · May 04, 2014 at 07:08 PM

Your code in the start function:

 elfFemale = GameObject.Find ("FemaleElf").transform.position;

Is getting the position when the script starts, but you need to get a reference to the objects transform so you can translate it.

 elfFemale = GameObject.Find ("FemaleElf").transform;
 elfMale = GameObject.Find ("MaleElf").transform;

Then in Update you can go:

 elfMale.position = Vector3.Lerp (elfMale.position, newPosition, Time.deltaTime * smooth);


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
1

Answer by rutter · May 06, 2014 at 11:31 PM

You're misunderstanding the parameter to Lerp. I'll quote an old forum post of mine to explain.

I usually just explain the lerp parameter as a percentage. Given a t from 0 to 1, you'll get a result that is t percent between a and b.

If you want a value that changes over time, t needs to change over time.

This example is in C#, but the fundamental considerations are identical:

 public float min = 0f;
 public float max = 100f;
  
 public float lerpTime = 1f;
 float currentLerpTime = lerpTime + 1f;
  
 public void StartLerping() {
     currentLerpTime = 0f;
 }
  
 void Start() {
     StartLerping();
 }
  
 void Update() {
     currentLerpTime += Time.deltaTime;
     if (currentLerpTime <= lerpTime) {
         float perc = currentLerpTime / lerpTime;
         float current = Mathf.Lerp(min, max, perc);
         Debug.Log("current: " + current);
     } else {
         Debug.Log("finished");
     }
 }

In your case, you're passing current Time.deltaTime to Lerp. That's only the time for the current frame. The above example instead uses a value which increases by Time.deltaTime once per frame, representing the total time that has passed since we started lerping.

Comment
Add comment · Show 1 · 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 robertbu · May 06, 2014 at 11:46 PM 0
Share

@rutter - the OP's original use of Lerp() is common and will work as long as the starting position is updated each frame. It results in an eased movement. It does it job by moving nearly the same fraction towards the goal each frame. Since the goal is getting closer each frame, the actual movement shrinks. The result is an eased movement. One downside of the OP's form of Lerp is that, though it gets close quickly, it takes a long time to reach the goal (start and end positions are deemed equal by Unity). It is like the old though question, if you move half the distance to your goal each day, how long does it take you to reach your goal. Answer: never. Your use of Lerp() is the traditional one and results in linear movement.

avatar image
0

Answer by Kobusvdwalt · May 03, 2014 at 11:50 AM

Try transform.position = vector3.Lerp();

Comment
Add comment · Show 1 · 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 CB-TV · May 04, 2014 at 06:59 PM 0
Share

Doesn't work.

avatar image
0

Answer by vipinsoni38 · Mar 03, 2020 at 07:53 AM

 public class CameraScript : MonoBehaviour
 {
 // Start is called before the first frame update
 bool isGoing;
 float starttime,fraction,time;
 Vector3 a, b;
 void Start()
 {
     isGoing = false;
     fraction = 0;
 }

 // Update is called once per frame
 void Update()
 {
     if (isGoing)
     {
         fraction = (Time.time - starttime) / time;
         Debug.Log(fraction + " a: " + a + " b: " + b);
         if (fraction > 1)
         {
             isGoing = false;
             return;
         }
         transform.position =  Vector3.Lerp(a, b, fraction);
     }
 }

 public void LerpIt(Vector3 a, Vector3 b, float time)
 {
     Debug.Log("Entered");
     starttime = Time.time;
     this.a = transform.position;
     this.b = b;
     this.time = time;
     isGoing = true;
     //transform.position = b;
 }

}

in my case, I was simply writing Vector3.Lerp();
the mistake was transform.position = vector3.lerp();

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 nu51313932 · Mar 03, 2020 at 06:45 AM

Re-check your object name.

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

28 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

Related Questions

iTween MoveTo shoots off into distance 0 Answers

Simultaneously moving GameObjects using C# script. 3 Answers

Cannot get game object to correctly follow mouse 2 Answers

How to cap this movement? 1 Answer

How to move character from one room to another? 1 Answer

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges