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 Gianluca · May 24, 2011 at 04:30 PM · object move

Need Update without... function Update

Hi,

I'm using unity from 5 days and I have some big difficulty with scripts, because I never used Java, so I try to ask here... maybe you can help me...

In the scene there are a Sphere and a Cube. The Sphere must move to Cube when I push a button.

This is the script:

 private var speed = 0.25;
 private var increment : float;
 
 function OnGUI () {
     // Build a Box
     GUI.Box (Rect (10,10,100,90), "Move");
 
     // Build the button. If push goto the function Move
     if (GUI.Button (Rect (20,40,80,20), "Cube")) {
         MoveSphere ();
        
     }
 
 }
 
 function MoveSphere () {

             // Look if the Sphere has a different position than Cube
             if (transform.position != GameObject.Find("Cube").transform.position) {
                 if (increment <=1) {
                 increment += speed/100;
                 }
 
                 // Move the Sphere to the Cube position
                 transform.position = Vector3.Lerp(transform.position, GameObject.Find("Cube").transform.position, increment/2);
                 
             }
             else {
                 print ("Finish!");
                 increment = speed/100;
                 return;
             }
 }

The first function creates a box and a button, if pushed begin the second function.

The problem is that the Sphere moves only a little distance, because need to repeat the function MoveSphere until the Sphere get the Cube position. I tried to write an other call to function MoveSphere (); after the transform.position=Vector3.Lerp etc. etc. but the execution is so fast than all the cycle of move happens in less time than a frame refresh, so the Sphere goes to Cube in 1 frame only. If I change the function MoveSphere () with the function Update () the Sphere moves right, like I want, but all begins when the script run and not when I push the button... and the function can't stop with return command... so the function continue to calculate the same transform.position forever.

How can I repeat the function MoveSphere every frame (or every 1/30 seconds) like happen with function Update ?

Thankyou very much for any help and really sorry for my bad english...

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

3 Replies

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

Answer by Byterunner · May 24, 2011 at 04:37 PM

Declare a variable that holds the value of whether the sphere should be moving or not. When you click the button, make that value true. In the Update function, test whether that value is true and if it is, move the sphere.

In addition to what you already have, add the following (replace your OnGUI).

 private var moving = false;
 
 function OnGUI () {
     // Build a Box
     GUI.Box (Rect (10,10,100,90), "Move");
 
     // Build the button. If push goto the function Move
     if (GUI.Button (Rect (20,40,80,20), "Cube")) {
         moving = true;
     }
 }
 
 function Update() {
     if (moving) {
         MoveSphere();
     }
 }
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
1

Answer by Meltdown · May 24, 2011 at 05:01 PM

Either way you are going to need Update() somewhere :p

I modified your script, and did the following, instead of increment in Lerp I used Time.deltaTime to smoothly move the sphere over to the cube...

 var moving : boolean = false;
 var increment : float;
 var speed : float = 0.25;
 
 function Update()
 {
     moving = true;
 
     if(moving)
         MoveSphere();
 }
 
 function MoveSphere() {
     // Look if the Sphere has a different position than Cube
     if (transform.position != GameObject.Find("Cube").transform.position) {
         if (increment <= 1) {
             increment += speed;
         }
 
         transform.position = Vector3.Lerp(transform.position, GameObject.Find("Cube").transform.position, Time.deltaTime / 2);
 
     }
     else {
         print("Finish!");
         increment = speed / 100;
         return;
     }
 }
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 Meltdown · May 24, 2011 at 05:03 PM 0
Share

Sorry to forgot to mention, you can set the variable moving = true in your OnGUI() method when clicking the button as Byterunner mentioned.

avatar image
0

Answer by Gianluca · May 24, 2011 at 05:32 PM

Really thankyou Byterunner and Meltdown! With this condition inside Update it works like I want!!!

For Meltown: I don't use Time.deltaTime because when the Sphere is more and more near to cube it goes more and more slow and need to much time to arrive... so it is fast in begin but too slow at the end. But maybe need some operation with deltatime and I can tune the velocity.

Thankyou for now :) Bye

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 Meltdown · May 24, 2011 at 05:38 PM 0
Share

Cool, remember to vote on an answer if it helps you and mark as answer the question that best answered your question. Also use comments on people's posts to reply to their answer, don't create a new 'answer' :p

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

i want my object to move when it collides with my player 2 Answers

Block Selecting and Moving 0 Answers

How to do that? 0 Answers

Why is my object not moving 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