- Home /
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); }
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
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.
so does the script you showed work at all? The code looks ok at a glance, what exactly is the problem?
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.
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.
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
Your answer
Follow this Question
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