Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 Macdude2 · Mar 09, 2011 at 05:37 AM · animationdoor

Animate Doors and Button Through Script

Alright, I have tried to create doors, a button, and a collider to trigger the button in order to make it so that if a player walks onto a button, it will open the doors. However, I would like the opening and closing to be animated with script and not an actual animation because I will be using many doors throughout my game and I do not think it is possible to make an animation non-position specific. To accomplish this I have tried to use lerp. This works when it goes without being activated, but when it is activated, the doors and button do not animate, but instead instantaneously change position. The script I use on the doors and button is below and the script I use on the button activator is below that. Thanks for any help.

* Code is fixed and now animates over several frames.

var start : Transform; var end : Transform; var ObjectSpeed = 1.0; private var Down = 0; private var startTime : float;

function ButtonDown() { startTime = Time.time; Down = 1; print("button Down"); }

function ButtonUp() { Down = 2; print("button Up"); }

function Update(){

     var dist = Vector3.Distance(start.position, end.position);
     var elapsedTime = Time.time - startTime;

 if(Down == 1){
         transform.position = Vector3.Lerp(start.position, end.position,elapsedTime*ObjectSpeed);

          if (dist <= 0)
           Down = 0;
     }

 if(Down == 2){
         transform.position = Vector3.Lerp(end.position, start.position, elapsedTime*ObjectSpeed);

          if (dist <= 0)
               Down = 0;
 }

}

This is the button activator script:

var RightDoor: Transform; var LeftDoor: Transform; var MyButton: Transform;

function OnTriggerEnter (other: Collider) { if(other.gameObject.CompareTag("Player") || other.gameObject.CompareTag("Box")) { print("yes"); MyButton.gameObject.SendMessage("ButtonDown", null); LeftDoor.gameObject.SendMessage("ButtonDown", null); RightDoor.gameObject.SendMessage("ButtonDown", null); } }

function OnTriggerExit (other: Collider) {

 if(other.gameObject.CompareTag("Player") || other.gameObject.CompareTag("Box"))

{ MyButton.gameObject.SendMessage("ButtonUp", null); LeftDoor.gameObject.SendMessage("ButtonUp", null); RightDoor.gameObject.SendMessage("ButtonUp", null); } }

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
0
Best Answer

Answer by loopyllama · Mar 09, 2011 at 10:49 AM

There are several issues going on here. Update() happens every frame. If you follow the logic: if down = 1 on, say frame 1, then the door starts to animate that frame and then door = 0. That means on frame 2 the door will not animate any longer because door = 0. You are only getting 1 frame of animation. To fix, you need to do something like this:

function Update() { if(Down == 1) { transform.position = Vector3.Lerp(start.position, end.position, Time.time*ObjectSpeed);

     if (transform.position >= end.position)
         Down == 0;
 }

...

This says "keep animating the door until the door position is greater than or equal to my end position".

Why is your door fully animating over 1 frame? because the Lerp method takes 3 parameters: start number, end number, and a 0 to 1 that controls the blending between the start number and end number. That is right! When that number hits 1 or greater you get the full value of the end number. You input Time.time ObjectSpeed as your 0 to 1 value. Time.time is the current time which will always be greater than 1. What you want is to check for elapsed time. You need to do something like this: startTime = Time.time in your Down function, declaring var startTime : float at the top of your file. Then in your Update function do something like var elapsedTime = Time.time - startTime. Then use elapsedTime ObjectSpeed instead of Time.time * ObjectSpeed.

Makes sense?

Comment
Add comment · Show 2 · 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 Macdude2 · Mar 09, 2011 at 04:02 PM 0
Share

thanks very much!

avatar image Macdude2 · Mar 10, 2011 at 04:12 AM 0
Share

All I had to do was compare the distance of the doors ins$$anonymous$$d of the start and end position, but now it works!

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

Trigger door animation with external trigger 1 Answer

Animation clip does not exist? 1 Answer

Door Movement Animation 3 Answers

Need help with my animation script 2 Answers

Respawn script and door animation help 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