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
0
Question by noahg1 · Aug 08, 2012 at 07:33 PM · transformarraylerpwaypoints

How to move along a group of waypoints at a certain speed?

I am trying to write code that records a players position as it moves, and when a key is pressed instantiates a copy that moves along the same path at the same speed. I have managed to make it work apart from moving at the same speed. when i press the key the character moves along the points to the current location in a fraction of a second. I have tried many variations but here is one version of the code:

var playerGO; var wayPoints = new Array (Vector3(0,0,0)); var playerLookAlike : Transform; private var playerLookAlike1: Transform;

function Start(){ playerGO = GameObject.FindWithTag("Player"); }

function Update () {

 wayPoints.Add(playerGO.transform.position);
 
 if (Input.GetKeyDown("z")){
     playerLookAlike1 =  Instantiate(playerLookAlike, Vector3(0,1.315133,0), playerLookAlike.rotation);
     
     for (i = 1; i < wayPoints.length; i++){
         playerLookAlike1.transform.position = Vector3.Lerp(wayPoints[i-1], wayPoints[i], 0.7);
         print (playerLookAlike1.transform.position);
     }
 }
 

} can anyone tell me how i can slow down the playerLookAlike so that is moves at the same speed that the player moved at?

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 ScroodgeM · Aug 08, 2012 at 08:32 PM

issue 1

function Update () {
    wayPoints.Add(playerGO.transform.position);

this is unusable data. Update() is called each frame rendered. it can be 300 frames per second or 3. so you will get 300 points or 3 - what will you do with all of them? wich time all points should play? and remember, that FPS is not a constant.

solution 1

store position in fixed time intervals. for example, each 0.1 second. code should look like:

//store time when we will store position
float timeOfNextCheckpoint;
//executes once at start
void Start()
{
    //initialize/reset time
    timeOfNextCheckpoint = Time.time;
}
//executes every frame
void Update()
{
    //check if it's time to store position
    if (Time.time >= timeOfNextCheckpoint)
    {
        //store position
        wayPoints.Add(playerGO.transform.position);
        //and calculate time for next storing
        timeOfNextCheckpoint += 0.1f;
    }
}

issue 2

Update() is method that called once a frame. it must contains some simple work that you need EVERY frame. in your task you need to make a long-time operation, so you can only start it in Update, but not make it all. your code makes move through all points in one frame.

solution 2

void Update ()
{
    if (Input.GetKeyDown("z"))
    {
        playerLookAlike1 =  Instantiate(playerLookAlike, Vector3(0,1.315133,0), playerLookAlike.rotation);
        //start a long operation
        StartCoroutine(ReplayCoroutine());
    }    
}
IEnumerator ReplayCoroutine()
{
    for (i = 1; i < wayPoints.length; i++){
        playerLookAlike1.transform.position = Vector3.Lerp(wayPoints[i-1], wayPoints[i], 0.7);
        print (playerLookAlike1.transform.position);
        //make a pause 0.1 sec
        yield return new WaitForSeconds(0.1f);
    }
}
Comment
Add comment · Show 4 · 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 Kryptos · Aug 08, 2012 at 08:36 PM 0
Share

Damn, you were faster ;)

avatar image noahg1 · Aug 08, 2012 at 08:54 PM 0
Share

thanks a lot! i tried putting in the yield but it hadn't occurred to me to slow down the amount of coordinates being added to the way points variable. again thanks a lot!

avatar image ScroodgeM · Aug 08, 2012 at 08:58 PM 0
Share

@kryptos, yep, and i like it 8) gogogo, it's still a lot of unanswered 8)

@noahg1, the main point in Coroutine is a time-delay, simple loop will not solve your issue

avatar image wondersonic · Aug 08, 2012 at 09:36 PM 0
Share

Wondering if iTween (http://itween.pixelplacement.com) could be used here.

avatar image
0

Answer by Kryptos · Aug 08, 2012 at 08:35 PM

You're changing the position in only one frame (one call to Update). You need to use a coroutine and yield between each change of position.

And your recording is also wrong because you have no idea how much time passed between each recorded waypoint. You could either use a coroutine with fixed time sample or record the deltaTime in a separate list.

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

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

10 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

Related Questions

How to move a gameobject with same speed 1 Answer

Determine transform.position of location between two waypoints on bezier path 0 Answers

Nested Prefab Issue 0 Answers

EditorGUILayout Array 1 Answer

object rotates when moving backward 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