Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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
1
Question by sona.viswam · May 29, 2013 at 09:21 AM · transformarraypositionlerp

How to move a gameobject with same speed

i have a gameobject its moving. in start

 speed = 10f;
 dirX = 1;
 dirY = 1;
 direction = transform.TransformDirection(new Vector3(dirX, dirY, 0));

in update

 transform.Translate(direction * Time.deltaTime * speed);

There are three points in game.

While i touch on gameobject it should move to firstpoint.

First point is 10f(distance) away from game object.

and secondpoint is 20f(distance) away from first point.

And third is 50f(distance) away from second point.

I kept all points in a arraylist.

Using lerp i am trying to move from update function.

How make it move with a uniform speed?

This is the code i written in update

 Vector3 nPoint = (Vector3)myPoints[index];
 this.transform.position = Vector3.Lerp(transform.position, nPoint,  0.4f);
 index++

the object is not moving with uniform speed? Do you have any other idea to make it work?

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

1 Reply

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

Answer by drawcode · May 29, 2013 at 09:34 AM

Set some values for speed and initial time

 float speed = 1.0f;
 float startTime = Time.time;

Get the distance between the two points currently in use (last and next)

 float distanceBetween = Vector3.Distance(lastPos, nextPos);

Then use that in the time of the lerp by calculating a relative time

 float distanceTime = (Time.time - startTime) * speed;

Then calculate the relative time

 float reltiveTime = distanceTime / distanceBetween;

Use that in your lerp

 this.transform.position = Vector3.Lerp(transform.position, nPoint,  reltiveTime);
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 drawcode · May 29, 2013 at 10:11 AM 0
Share

print out relativeTime and ensure it is a good value. Also you might have to move part of this out of update (starttime should only be set when you reach a point for instance, same with distance beween). See this example: http://docs.unity3d.com/Documentation/ScriptReference/Vector3.Lerp.html

avatar image drawcode · May 29, 2013 at 10:13 AM 0
Share

Since you have an array of points, you'll need to change startPosition and endPosition that you want to calculate when each point is reached. The example above it between two points only. Or just do the two points and then dynamically set the start and end position from another script according to your array of positions.

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

14 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

Related Questions

problem moving a prefab object in script (c#) 0 Answers

Turning character slowly with Slerp 1 Answer

move a gameobject to left/right while going up 1 Answer

Unity3D - Playback object array of position (with dynamic velocity) 0 Answers

Smooth Lerp movement? 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