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
2
Question by tomazsaraiva · Mar 03, 2010 at 01:32 PM · gui-button

Using a GUI button to stop a gameobject movement

Hi,

I'm trying to use a GUI button to stop the scripted movement of a gameObject.

This is the deal: I have an elevator moving from one floor to another when the player presses a button on the elevator panel. I'm using this code:

while(elevatorHeight > triggerHeight) { elevatorHeight = Mathf.Ceil(elevator.transform.position.y); triggerHeight = Mathf.Ceil(trigger.transform.position.y); elevator.transform.Translate(Vector3(0, 0 , -speed*Time.deltaTime)); yield; }

 while(elevatorHeight < triggerHeight)
 {
     elevatorHeight = Mathf.Floor(elevator.transform.position.y);
     triggerHeight = Mathf.Ceil(trigger.transform.position.y);
     elevator.transform.Translate(Vector3(0, 0 , speed*Time.deltaTime));
     yield;
 }

Now I need to create a GUI button able to stop the elevator movement at any point and then resume it's movement.

How can I do this?

Thanks for any help

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
3

Answer by Motionreactor · Mar 03, 2010 at 02:51 PM

Ok, I've written a complete script for you because I thought it important to help illustrate a more flexible approach to the problem:

Drop this script onto the elevator:

var elevatorSpeed : float; var floor1Height : float; var floor2Height : float; var floor3Height : float;

private var elevator : GameObject; private var elevatorActive : boolean; private var elevatorDestination : float;

function Start() { // initialise variables which must have a value, otherwise errors ensue elevator = this.gameObject; elevatorActive = false; elevatorDestination = elevator.transform.position.y; }

function Update() { if(elevatorActive) { MoveElevator(); } }

function MoveElevator() { // Move the elevator elevator.transform.position.y = Mathf.Lerp(elevator.transform.position.y, elevatorDestination, elevatorSpeed * Time.deltaTime);

 // Disable the elevator if it is within a very small margin of the destination, ie it is 'close enough' so stop
 if(Mathf.Abs(elevator.transform.position.y - elevatorDestination) < 0.01)
 {
     print("stopped elevator");
     elevatorActive = false;
 }

}

function GoToFloor(floorNum : int) { switch(floorNum) { case 1: elevatorDestination = floor1Height; break; case 2: elevatorDestination = floor2Height; break; case 3: elevatorDestination = floor3Height; break; default: elevatorDestination = floor1Height; break; }

 elevatorActive = true;

}

function StopElevator() { elevatorActive = false; }

function ResumeElevator() { elevatorActive = true; }

Now all you have to do is set the speed and floor variables in the component.

To 'trigger' the elevator from another script on another object, such as a trigger of course:

GameObject.Find("Elevator").GetComponent("ElevatorScript").GoToFloor(1);

...and of course emergency stop:

GameObject.Find("Elevator").GetComponent("ElevatorScript").StopElevator();

...and resume from stopped position (even if halfway between floors):

GameObject.Find("Elevator").GetComponent("ElevatorScript").ResumeElevator();

Voila! ...and all without a pesky while() loop in sight! (nasty things for beginners!)

An alternative way to move the elevator with a clamped speed, limited to a maximum is a case of changing a variable:

var elevatorSpeed : float;

becomes:

var elevatorMaxSpeed : float;

...and the move function :

function MoveElevator() { // Calculate difference between current position and desired position var elevatorVelocity = elevatorDestination - elevator.transform.position.y;

     // Limit the velocity by clamping it to positive or negative maximum velocity
     elevatorVelocity = Mathf.Clamp(elevatorVelocity, -elevatorMaxSpeed, elevatorMaxSpeed);

     // Move the elevator
     elevator.transform.position.y += elevatorVelocity * Time.deltaTime; // += accomodates positive or negative movement and Time.deltaTime gives us some smoothing

     // Disable the elevator if it is within a very small margin of the destination, ie it is 'close enough' so stop
     if(Mathf.Abs(elevator.transform.position.y - elevatorDestination) < 0.01)
     {
         print("stopped elevator");
         elevatorActive = false;
     }
 }

I set the max speed to a fairly low value 0.5 in my case.

Comment
Add comment · Show 9 · 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 Motionreactor · Mar 03, 2010 at 02:52 PM 1
Share

I may have used some things unfamiliar to you, let me know if you need help deciphering this.

avatar image Lipis · Mar 03, 2010 at 03:15 PM 0
Share

@$$anonymous$$otionreactor, Nice and elegant... :) I need to start using $$anonymous$$athf.Lerp like.. everywhere :)

avatar image Motionreactor · Mar 03, 2010 at 03:19 PM 0
Share

I still find Lerp uncomfortable to use, and have to frequently check how to use it. For years I've been writing my own interpolation in other languages, so using Lerp feels like cheating!

avatar image Lipis · Mar 03, 2010 at 03:28 PM 0
Share

@$$anonymous$$otionreactor unfortunately I can't test this script right now.. and never really used Lerp in Unity yet.. Can you tell me if the elevator is moving at the exact speed all the time..? (Is speed the same when going from f1 > f4 with going f1 > f2)

avatar image tomazsaraiva · Mar 03, 2010 at 03:35 PM 0
Share

Thanks a lot..I'm gonna test the script in a $$anonymous$$ute, I will give you some feedback in a while..thanks once again

Show more comments
avatar image
1

Answer by Ashkan_gc · Mar 03, 2010 at 01:48 PM

you should write something like this

function OnGUI ()
{
 if (GUI.Button (Rect(30,30,100,100),str) == true)
 {
  if (str == "pause")
  {
   str="resume";
   speed=0;
  }
  else
  {
   speed=10;
   str="pause";
  }
 }
}

you should define the str string var in your script and set it's default value to "pause". 10 is a sample value for speed. set it to what you want.

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 Roux69 · Aug 10, 2010 at 08:48 PM 0
Share

I understood $$anonymous$$otion's script perfectly... but I must admit yours eludes me... for example, indentation would be a nice addition to the clarity of your script

avatar image
1

Answer by duck · Mar 03, 2010 at 01:48 PM

You should probably have your button panels set a variable on your elevator which is the "destination height". Then, make it so your elevator is always trying to reach the destination height if it's not already there. (It looks like you may already have something like this in your "triggerHeight" variable). If you rename it to "destinationHeight" it might make more sense.

You could then cancel the movement of the elevator by simply setting the "destinationHeight" to the elevator's current height.

Comment
Add comment · Show 3 · 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 duck ♦♦ · Mar 03, 2010 at 01:51 PM 0
Share

Oh wait, that would make it so it wouldn't resume :D

avatar image tomazsaraiva · Mar 03, 2010 at 01:56 PM 0
Share

I have a trigger on each floor, thats why I'm using the triggerHeight variable. The script compares the elevator position with the trigger it's trying to reach and moves it up or down until it reaches the destination.

I understand that by setting the elevator position to the same as the trigger, the elevator would stop. The problem is that I have 5 different triggers (5 floors), and the elevator could be trying to reach any of them.

So should I put that code on the elevator script or the GUI? If it's in the elevator script, how can I link it with the GUI button?

Thanks a lot

avatar image duck ♦♦ · Mar 03, 2010 at 02:15 PM 1
Share

I think Ashkan's solution of setting the speed to zero is a better idea, given the requirement of being able to restart the motion towards the target.

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

No one has followed this question yet.

Related Questions

My Buttons are dissapearing 0 Answers

GUI Button not displaying an icon 3 Answers

Is it possible to detect which GUIStyleState is used? 2 Answers

GUI Button make text bigger. 2 Answers

Touches aren't working on Android 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