• 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 Merrick · Mar 27, 2014 at 03:49 PM · scripting problem

Basic Lerp question

Hi everyone, I'm extremely new with Unity and scripting.

I have an object that when it apear I need it to go from world origin to the specific Vector3 (0,0,8)

But I can't seem to find the mistake I'm doing. This is the script that I wrote for the object. Is pretty simple:

{ public float speed;

 void start ()
 {
     transform.position = Vector3.Lerp(transform.position, new Vector3 (0,0,8), Time.deltaTime * speed);
 }


}

I also tried with a void awake and nothing...

Help! Anyone?

Comment
Add comment · Show 4
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 Destran · Mar 27, 2014 at 03:51 PM 0
Share

try giving speed a value like public float speed = 3. and also start needs to be capitalized like Start()

avatar image Lo0NuhtiK · Mar 27, 2014 at 03:54 PM 1
Share

Also, put your lerp in Update() or use a coroutine since Start() is only ran once.

avatar image Destran · Mar 27, 2014 at 03:58 PM 0
Share

That too ^^

avatar image Professor Snake · Mar 27, 2014 at 04:08 PM 0
Share

You also need to capitalize the s in Start.

1 Reply

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

Answer by Curb · Mar 27, 2014 at 04:07 PM

The Start function will only execute on one frame. A lerp is usually used to animate an object over multiple frames. One way of doing this would be to make a coroutine:

 IEnumerator MoveObject(float duration){
     float t = 0;
     
     Vector3 currentPos = transform.position;
     Vector3 newPos = new Vector3(0,0,8);
 
     while (t < 1){
         t += Time.deltaTime / duration;
         transform.position = Vector3.Lerp (currentPos, newPos, t);
 
         yield return null;
     }    
 }

Then call it from somewhere with:

 StartCoroutine(MoveObject(3));

The 3 is the duration of the animation. You could build in even more parameters between the () of course.. such as the new position. But this should get you going. :)

Comment
Add comment · Show 3 · 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 Merrick · Mar 27, 2014 at 06:39 PM 0
Share

This worked great!! It's exactly what I wanted. Can I ask you why the rutin is a IEnumerator? I thought that was for geting aligned a series of numbers.

avatar image Merrick · Mar 27, 2014 at 06:49 PM 0
Share

TY all for your answers

avatar image Destran · Mar 27, 2014 at 06:51 PM 0
Share

it's an IEnumerator because it's a Coroutine. Any function with a yield statement is a coroutine. Don't forget to mark Curb's answer as correct :D

http://docs.unity3d.com/Documentation/$$anonymous$$anual/Coroutines.html

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

24 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

Related Questions

Scope issues when initializing variables 2 Answers

Audio delay after loading ogg external movie at runtime 0 Answers

Change two shader color with script 0 Answers

how can i make a working slingshot or launchpad script 1 Answer

OnMouseDown only works once 1 Answer

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