Amnesia-like shelf-opening script, Help!

Hey guys.

I wrote about this yesterday, but I kinda think the title of the topic did not explain my problem
very well, so here I go again.

Hello guys!

The title of this topic pretty much tells what my problem is.

I’m trying to create a script that makes you able to open a shelf by clicking on it, and move the mouse
pretty much like in the game Amnesia.

I’m doing a raycast and everything seems to be right up until now. However I have a problem.

When I’m doing the raycast I’m checking for a mouse input when I’m pointing at a shelf, and if
I am indeed looking at one, and click it, I’m storing the mouse position in a variable.
I then check if the mouse position changes when the mouse button is held down, and if it is,
I transform the translate, making the shelf come out of the drawer.

Sadly, it doesn’t quite work the way I’m currently doing it.

This is the part of my script dealing with this:

Code:

(...)

        else if( hit.collider.tag == "Skuffe" )

        {

            if( Input.GetMouseButton(0) )

            {

                var mousePosX = Input.mousePosition.x;

                if( Input.mousePosition.x > mousePosX )

 

                {               

                    hit.collider.gameObject.transform.Translate(0, 0, .1 * Time.deltaTime);

                }

                Debug.Log(mousePosX);

            }

        }

I do realise that I’m comparing two variables that are the same, but I just didn’t know where to
go from there.

Anyone have an idea how to fix this? Or maybe a better idea for the same functionallity?
The way I wanted to try and do this was to store the current mouse possition and compare it
to any time when the mouse button is pressed, to see if it differs from when the mouse button
was first pressed, and then act opun that - But how can I do this?

If I understand your needs, you want to move the shelf on mouse delta, so I think that it’s probably easier to use Input.GetAxis
Also about easier, I would move this logic to an Update attached to the shelf itself (just execute the code based on a flag that is enabled in OnMouseDown and disabled in Update when Input. GetMouseButtonUp) to get rid of making raycast each frame.

Thanks for your reply! - I’ll try it.

If anyone have got any other suggestions, please do tell.:slight_smile: