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 Mz3D · Jul 11, 2012 at 09:32 AM · movementinstantiatevector3random.range

Object movement - undesired effects

Hello everyone! I'm new to unity and i'm trying to realize a simple thing: i want to make an object move from point A (a Vector3) to point B (another Vector3 but with a random x value), then when it reaches point B, i would like it to start moving left and right. I've created this code, but it generates two undesired effects: 1) If the values of the random range of the x axis are not set from 0 to 1 (which is a single value) the object in his movement from point A to point B doesn't follow a linear trail, it flickers rapidly and seems to move in various directions at the same time. 2) When the objects reaches point B (the value 0 on the z axis) it is rapidly moved some units to the right, then it starts the correct "PingPong" movement. Can anyone help me?

 var enemySpeed : float;
 var start : Vector3;
 var speed : float;
 
 function Update () {
  transform.position = Vector3.Lerp(start, Vector3(Random.Range(-4,4),0,0), Time.time*speed);
  if (transform.position.z == 0){
  transform.position.x = Mathf.PingPong(Time.time * enemySpeed, 9);
  transform.position.z = Mathf.PingPong(Time.time * enemySpeed, 1);
  }
 }


In another script i have this line of code that instantiates the object, maybe it can help solving the problem:

 Instantiate(enemies[0], Vector3(0,0,15), transform.rotation);

The values of this Vector3 and the "start" Vector3 variable are the same.

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

Answer by whydoidoit · Jul 11, 2012 at 09:37 AM

You are allocating a new random value every Update (every frame) - you need to choose the position to Lerp to in Start and then use it in update. It strikes me that your ping pong is starting from a fixed position so there will be a big jump when it kicks in. You probably want to add the ping pong to the position that was chosen for the Lerp.

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 Mz3D · Jul 11, 2012 at 10:08 AM 0
Share

I've tryed to change the code as this:

 var newpos : Vector3;
 
 function Start(){
  newpos = Vector3(Random.Range(-4,4),0,0);
 }
 
 function Update () {
  transform.position = Vector3.Lerp(start, newpos, Time.time*speed);
  if (transform.position.z == 0){
  transform.position.x = newpos.x + $$anonymous$$athf.PingPong(Time.time * enemySpeed, 9);
  transform.position.z = newpos.z + $$anonymous$$athf.PingPong(Time.time * enemySpeed, 1);
  }
 }

Now the flickering problem is solved (thank you!) but the strange jump to the right (when the objects reaches point B) still remains. What can i do?

avatar image whydoidoit · Jul 11, 2012 at 10:13 AM 0
Share

I've converted your answer to a comment - on UA Answer means Solution and not Reply :) There's a hidden Add New Comment button on the right of the screen.

The problem with the jump is that Time.time is greater than 0 when you get to z == 0 (it's the number of seconds since the game started, the way you have it written you will reach newpos after 1 second so you can just take 1 off Time.time in the second bit). The script reference is rubbish on this stuff - really you should maintain your own float variable and add Time.deltaTime to it - as this stops there being problems if you ever want to start this kind of code again or not have it happen instantly as the game runs.

 var newpos : Vector3;
 float startingTime = 0;
 float pingPongTime = 0;
  
 
 function Start(){
  newpos = Vector3(Random.Range(-4,4),0,0);
 }
 
 function Update () {
  startingTime += Time.deltaTime;
  transform.position = Vector3.Lerp(start, newpos, startingTime*speed);
 
  if (transform.position.z == 0){
  transform.position.x = newpos.x + $$anonymous$$athf.PingPong(pingPongTime * enemySpeed, 9);
  transform.position.z = newpos.z + $$anonymous$$athf.PingPong(pingPongTime * enemySpeed, 1);
  pingPongTime += Time.deltaTime;
  }
 }
avatar image Mz3D · Jul 11, 2012 at 10:25 AM 0
Share

Problem solved! Thank you! For now i haven't understood very well this solution, i need some time to study it! :)

avatar image Mz3D · Jul 11, 2012 at 10:38 AM 0
Share

Let me see if i've understood. Using Time.time Unity calculates the PingPong position values as the PingPong function would had started from the beginning of the game? So i had to reset time to 0?

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

find GameObject and move towards it, help please? 0 Answers

Instantiate objects at random positions on axis 2 Answers

2D Vector Movement,Vectorel Movement with Buttons 2 Answers

Randomizing a selection from an array 1 Answer

Player Instantiate rotaion problem 0 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