• 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 kushgodofhope · Feb 23, 2014 at 09:47 AM · javascriptlerptransform.position

transform.position vector3.lerp appears to stay still for a while before the 5DP matches the new location

My goal is to make an enemy who moves to a location, shoots at the player, waits a few seconds and then repeats the process for 3-5 times before it becomes vulnerable. i just finished scripting the movement, however, when the enemy moves to the new location, it isnt actually at that location until the 5 or so decimal points of each x,y,z position of the current location is in line with the new location. this makes it appear to be at the location for about 5 or so seconds, sometimes 10+ seconds, before moving. Below is the script im using.

 var newLoc : Vector3;
 var curLoc : Vector3;
 var speed : float = 100;
 
 function Start()
 {
 newLoc = Vector3(Random.Range(1.75,8.25),Random.Range(13.5,15.5),0);
 }
 
 function Update()
 {
 curLoc = transform.position;
 
 if(curLoc == newLoc)
 {
 newLoc = Vector3(Random.Range(1.75,8.25),Random.Range(14.5,15.5),0);
 }
 else if(curLoc != newLoc)
 {
 transform.position = Vector3.Lerp(curLoc, newLoc, Time.deltaTime * speed);
 }
 }

i tried to round the locations to 2decimal points to help speed up the lerp process, however i couldnt figure out how to do that to the current position (transform.position) of the object. heres the code i used to set newLoc to 2dps

 newLoc = Vector3(Mathf.Round(Random.Range(1.75,8.25)*100)/100,Mathf.Round(Random.Range(14.5,15.5)*100)/100,0);


What can i do to fix the problem of waiting in the same spot for it to realize im already there before it moves again?

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
0
Best Answer

Answer by robertbu · Feb 23, 2014 at 09:56 AM

The problem is an issue when Lerp() is used this way. This form of Lerp() moves towards the goal some small fraction of the distance each frame. The fraction varies a bit because Time.deltaTime varies, but essentially you can think of it as the same fraction each frame. As the distance drops, the same fraction of the remaining distance represents ever decreasing movement each frame.

It's like the old question: if I walk half the distance to my goal, when will I reach my goal? The answer: never.

There are two easier solutions to your problem, and many more complex ones. The easiest solution is to add a distance check and move the object to the goal if the distance falls below some threshold. The object reaches close to the goal relative quickly:

 if (Vector3.Distance(curLoc, newLoc) < 0.02) {
     transform.position = newLoc;
 }

Adjust the '0.02' as appropriate to your situation.

The second easy solution is to change from using Vector3.Lerp() to using Vector3.MoveTowards(). MoveTowards() does not ease the movement, but since it moves at a fixed speed, it arrives promptly (with respect to the whole movement) at the destination. All you have to do is change Lerp() to MoveTowards() and adjust the 'speed' parameter as appropriate.

If neither of these two solutions work for you, I'll present a more complex solution.

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 kushgodofhope · Feb 23, 2014 at 01:07 PM 0
Share

Thankyou very much for clarifying, the first solution is was exactly what i was looking for, works great.

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

19 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

Related Questions

Setting Scroll View Width GUILayout 1 Answer

Can someone help me fix my Javascript for Flickering Light? 6 Answers

How to lerp an object's scale? 2 Answers

Make Lerp or other more fluid or continuous 3 Answers

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

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