- Home /
DragRigidBodies, Mouse Centering and a custom pause menu. They all hate each other.
Hey,
I have the DragRigidBodies script on my FPController that also has MouseLook, and I hate how the mouse moves all over the place so you have to dance with it for awhile to pick something up, so I tried adding screen.lockCursor, which although meant the cursor wouldn't fly off out of the game window, it wouldn't stay centered in the screen (to make it easier and simpler to pick stuff up/interact with items with the mouse), you still had to dance with it to get the cursor in the right place. So I tried this loop:
function update (){
Screen.lockCursor = true;
Screen.lockCursor = false;
}
This was a moderately successful (albeit hacky) way to keep the mouse in the exact middle of screen, and both DragRigidBodies and any other 'OnMouseEnter' scripts worked great, so I was happy, until I then wrote a Pause Menu script, which has to use
function Start (){
Screen.lockCursor = false;
}
so that the player is free to click stuff. This is a problem because the loop is prioritized (regardless of it's place in the inspector or what it's attached to) and will not let the mouse move on the pause menu.
So, after all that, I have just one question. 'What is the best way to go about making all this compatible?' I was thinking one of three things, but I have no idea how to implement any of them.
1)Edit the DragRigidBodies and other 'OnMouseEnter' scripts so they activate when a GUI reticule/crosshair is over them, sort of like pretending the GUI IS the mouse (This is my preferred choice).
2)Finding some way to keep the mouse restrained to the very center of the screen without the lockCursor function.
3)Making a pause menu that uses key presses or in some way works WITH the constrained mouse, or making the pause menu override the other script.
Thanks everyone, and I'd like to quickly point out that I have literally scoured nearly every forum and answers pages that could be relevant, but most say that the mouse is read-only, and can't be made to do things. All the pause menu scripts and help pages I found use the mouse to navigate, and searching for things like 'activate on GUI over' or 'GUI to object recognition' brings up results that discuss how to set up GUI buttons or similar topics.
Thanks, Matt
P.s Sorry that it's huge, but I wanted to make sure I made clear my request and exactly what I wanted, as well as making sure people know I have tried everything before posting this. Thanks
The answer is: Write them yourself ins$$anonymous$$d of using other peoples stuff :)
Hey, I know a bit about Unity's JS (enough to manage) and mostly do write my own scripts, including the pause menu that is one of the problems, but I have no idea what functions I would use for my potential solutions. Is there a onGUIOver function? Can the mouse cursor be controlled? etc. etc. etc.
And why would unity offer these scripts if we were supposed to ignore them and not use them?
I'm not looking for someone to write the whole script for me, or nothing like, I'm just hoping that someone with more experience would be willing to push me in the right direction.
P.s Does my writing carry a certain tone? I find that in most forums or places like this I receive hostile or exasperated answers. I was wondering if it is me and my writing style, or I'm just stupid.
If you want to move the mouse, you can simply make a fake mouse.
Unity offers these scripts as examples. They can be used in many different ways, but are in most cases to advanced or complicated for specific use. That's why I write everything myself :)
You do come off like a complete noob, but note I was not being hostile. Just offering my opinion on the subject. Therefore the comment :)
Thanks Ben. I'll leave the game alone for a bit and learn some more JS. I think you're right, mixing and matching code (either my own or other peoples) rarely seems to turn out well, so cheers for the advice. :) Thanks again.
Answer by Jamora · Jun 01, 2013 at 08:51 AM
I would attempt to solve this problem by a RayCast from the screen center (or where ever the crosshair is) and checking whether or not it hits anything. I'm not sure how well dragging works with raycasts, but you could, possibly, remember which object was hit and then move it with the mouse possibly hidden with Screen.showCursor = false
.
Have a look at https://docs.unity3d.com/Documentation/ScriptReference/Physics.Raycast.html .
Cheers Jamora, this is brilliant! I knew I would have to use raycast, and Ben's idea of a fake mouse works perfectly with it. I'll go find out how to write JS a bit more. $$anonymous$$aybe if I had I wouldn't have needed to ask the question, but thanks all the same!
Your answer
![](https://koobas.hobune.stream/wayback/20220613103010im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
changing from mouse click to keys 0 Answers
FPC Mouselook Axis problem on pause GUI 1 Answer
Draw GUI Text if clicked out of webplayer 1 Answer
Timer Pause 1 Answer
Examples of TBS/Heavily Gui-driven Games in Unity? 2 Answers