Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 Finjitzu · Sep 02, 2011 at 11:30 AM · movementarraylerpdeltatime

Lerp Movement Pauses after frame

Hey everyone, I've been trying to sort this out for the last 6 hours. Maybe you can help me. In my Update function I have an input cmd:

if(Input.GetButtonDown("Walk")){
     CurrentUnit = true;
     Play();
     CurrentUnit = false;
    }

Then in my Play function:

function Play () {
//Debug.log(Team1Unit1Origin.GetType);
//collect all waypoint game objects
var waypoints = new Array (GameObject.FindGameObjectsWithTag("Waypoint"));
    //loop through waypoint array
    for (i = 0; i < waypoints.length; i++){    
        //variable for waypoint vector
        var newposition = waypoints[i].transform.position;
        //set time scale
        t = Time.deltaTime * 5;
        //move unit to first waypoint in array
        Team1Unit1.transform.position = Vector3.Lerp(Team1Unit1.transform.position, newposition, Time.DeltaTime * 5);
    }
The problem I'm having is that the Unit stops after each frame, so I have to keep tapping the Walk button for it to continue.

I'm not quit sure what I'm doing wrong, if anyone could help that would be great.

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

2 Replies

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

Answer by aldonaletto · Sep 02, 2011 at 12:27 PM

If you want the object Team1Unit1 to move to all waypoints in sequence when you press "walk", Play must be a coroutine where the object Lerps from the current position to the new waypoint at a defined speed, then change the waypoint and Lerps to it, and so on till the last one. The variable walking is set at the beginning of Play, and only cleared at the end - it's used to avoid other Play cycles being initiated while one is running.
I changed the creation of the waypoints array to Start, because Find operations are too slow (the docs say we should use them only at Start/Awake).

var speed: float = 3; // speed = 3 m/s private var waypoints: GameObject[];

function Start(){ waypoints = GameObject.FindGameObjectsWithTag("Waypoint"); }

function Update(){ if (Input.GetButtonDown("Walk")){ Play(); // starts Play cycle and returns immediately } }

private var walking = false;

function Play () { if (walking) return; // don't start a new cycle until this one has finished walking = true; // cycle started //loop through waypoint array for (i = 0; i < waypoints.length; i++){ // get next waypoint var newposition = waypoints[i].transform.position; // save start position var oldPos = Team1Unit1.transform.position; // calculate factor to walk at defined speed var spd = speed / Vector3.Distance(oldPos, newposition); for (var t:float=0; t<1;){ // sweep from oldPos to newPosition t = Time.deltaTime * spd; // ensures motion at defined speed // move object a little each frame Team1Unit1.transform.position = Vector3.Lerp(oldPos, newposition, t); yield; // return here next frame } } walking = false; // cycle ended }

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 Finjitzu · Sep 02, 2011 at 12:36 PM

HaHa yeah I just got it to work after finding this post:

http://answers.unity3d.com/questions/160549/click-on-object-and-move-with-lerp.html

Seriously after like 6 hours of trying different things. I know how to program, but I don't know how games work :P

I'm not sure if I can populate the waypoint array in the function start though. The player is able to spawn as many waypoints as they like. Then the play function gathers the waypoints and moves the player. Think Frozen Synapse. Perhaps I can populate the array on the user pick? Then send that array to the MainGameScript.

Thanks so much for your help, I'm loving this!

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 aldonaletto · Sep 02, 2011 at 03:27 PM 0
Share

You actually don't need to do that at Start - the docs recommend this because FindGameObjectsWithTag is a slow operation, but it's ok to use it at the Play routine beginning since it will be called rarely.

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Getting a 2d Sprite to move over time to an Array 1 Answer

Move continuously through array of Vectors 1 Answer

Jump with mathf.lerp problem 2 Answers

Smooth rotation... 1 Answer

Make Lerp or other more fluid or continuous 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