Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 /
avatar image
0
Question by JorgenFreeman · Nov 02, 2012 at 09:18 PM · lerpissuesc

Lerping issues (C #)

I'm attempting to move an object from one place to another, so I used a lerp but it isn't working.

Here's the function i created to run the lerp and also the instantiate:

attackObj = Instantiate (attackObj,attackingCity.transform.position,Quaternion.identity) as Transform;

attackObj.transform.position = Vector3.Lerp (attackingCity.transform.position,defendingCity.transform.position, Time.deltaTime);

Debug.Log ("Success!");

I'm running this function from update in an if statement for when I press space. It definately runs because the log works. It also does spawn the object on the attacking city, but doesn't move it at all. Any suggestions?

Comment
Add comment · Show 2
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 JorgenFreeman · Nov 02, 2012 at 09:22 PM 0
Share

I've played around with the numbers a little and the position of the attackObj changes when I enter a value between 0 and 1. I haven't really used Lerp in a while so i'm a little rusty at it.

avatar image Dave-Carlile · Nov 02, 2012 at 09:29 PM 0
Share

The final parameter needs to move from 0 to 1. At 0 it will return the start position, and at 1 it will return the end position.

2 Replies

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

Answer by Dave-Carlile · Nov 02, 2012 at 10:02 PM

Moving the t parameter in Vector3.Lerp from 0 to 1 over time will cause the object to move from the start position to the end over that same length of time.

Alternatively, you can pass in the position of attackObj as the start position, then use the t parameter as a damping value. So...

 attackObj.transform.position = 
    Vector3.Lerp(attackObj.position,
                 defendingCity.transform.position,
                 10 * Time.deltaTime);

The value 10 here can be modified to control how fast the object moves. What this does is move the object from its current position to the target position by 10 Time.deltaTime* percent of the distance. As it gets closer it will move less of a distance. Increasing the multiplier will cause the object to move a greater percentage of the distance each frame, but it will stop much faster (i.e. not as smoothly) when it arrives.

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 JorgenFreeman · Nov 03, 2012 at 02:01 PM 0
Share

Ah I see, i was using the attacking city's transform as the first argument rather than the attackObj's. Thanks for the help, it works fine now :D

avatar image
0

Answer by Anxo · Nov 02, 2012 at 09:27 PM

When you are using attackObj = Instantiate, You are creating an instance of an object and naming it attackObj. If you are doing that in Update function, it will create an instance every frame. So it constnatly creates new transforms and names them attackObj.

When you are telling it to lerp, you are telling the attackObj to lerp but next frame the attackObj is a different object. So you are starting the lerp on one object every frame but never continuing the lerp on one object over multiple frames. If you move the instanciate into start you will see it lerp.

Comment
Add comment · Show 2 · 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 JorgenFreeman · Nov 02, 2012 at 09:34 PM 0
Share

The code I typed above is in a function outside of the update which is only called when I hit space.

avatar image JorgenFreeman · Nov 02, 2012 at 09:41 PM 0
Share

Also, will this be able to move the object smoothly, or is a lerp just for instant jumping from one place to another?

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

11 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

Related Questions

Trouble referencing variables inside of a script (C#) 1 Answer

Trouble setting a variable to a game object using instantiate. 1 Answer

Getting parameter values sent to a method c sharp script 0 Answers

Error when trying to parse csv file 1 Answer

How to move an object from point A to point B with one key press 3 Answers


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