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
-1
Question by TakMarche · Jul 04, 2012 at 11:30 PM · java

Simple Java Script for Opening and Closing a Drawer

I'm pretty new to Java scripting myself and I've been searching up quite a few references already to help me build a script that can open and close a drawer (ex. a desk drawer) and show the animation smoothly without recording/playing any animation clips what so ever. I'm pretty lost right now on what to do but my script so far allows me to move the object toward the camera and moves it back to the position it started from.

var wasClicked : boolean; var start : Transform; var end : Transform; var smooth = 5.0;

private var startPoint : Vector3; private var startTime : float;

function Start() { startPoint = transform.position; startTime = Time.time; }

function OnMouseOver() { if(Input.GetMouseButtonDown(0) && wasClicked == false){ wasClicked = true; Activate(); } else if (Input.GetMouseButtonDown(0) && wasClicked == true){ wasClicked = false; Deactivate(); } }

function Activate() { transform.position = Vector3.Lerp(start.position, end.position, Time.deltaTime * smooth); }

function Deactivate() { transform.position = Vector3.Lerp(start.position, end.position, Time.time); }

Comment
Add comment · Show 6
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 AlucardJay · Jul 05, 2012 at 12:40 AM 0
Share

You could try using Raycast ins$$anonymous$$d of On$$anonymous$$ouseOver. Here's a similar question I posted an answer for : http://answers.unity3d.com/questions/274015/point-and-click-door-opening.html

avatar image TakMarche · Jul 05, 2012 at 05:33 AM 0
Share

Thanks alucardj but what I noticed with your similar question is that it also plays an animation. What I'm looking for is to build a script that doesn't require animation and can be use with just the vectors. Or just moving one object from one location to another but showing that it moved and not disappearing and reappearing.

avatar image Seth-Bergman · Jul 05, 2012 at 05:52 AM 0
Share

so does the script you showed work at all? The code looks ok at a glance, what exactly is the problem?

avatar image TakMarche · Jul 05, 2012 at 06:43 AM 0
Share

I updated the code a little bit to make it cleaner but the same thing was applied. What I'm trying to figure out is that when I click on the object with the current script it move to the location I want it to and moves back when I click on it again. Ins$$anonymous$$d of showing it instantly (ex. click a cube and it disappears and reappears at the spot I want it to) I want it to show the transition like it being moved there if you get what I'm trying to say.

avatar image JasonB · Jul 05, 2012 at 06:45 AM 0
Share

Is there some particular reason that you won't use animations? Especially since you're intentionally making the problem more difficult by trying to animate in code.

Show more comments

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Myhijim · Jul 05, 2012 at 12:05 PM

Not completely sure what you want. You could try looking around, like at the follwing?

http://answers.unity3d.com/questions/14279/make-an-object-move-from-point-a-to-point-b-then-b.html

Hope that helps, you could attach that to the drawer.

~ Myhijim

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

8 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

enable / disable particle system 1 Answer

rotation.y gets wrong data 1 Answer

Random Java Script Activation? 1 Answer

No headbob when jumping 1 Answer

UnityScript error in transform.Position 2 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