- Home /
Mouse Controller
I have been trying to get a mouse controller to work for a 3D puzzle game. I need to drag prefabs around the screen, and I want to drag only in X Y coordinates by default with no rotation. While dragging I want to press W to enable a forward backward movement on the Z coordinate. Finally I want to be able to press Shift and enable only Object rotation. I have been working with the DragRigidBody.cs script that comes with Unity, I just don't know how to change to code to do what I want it to do.
Have a look in the Unify Community website. There are ALOT of very handy scripts. Here is a link to the controller page : http://wiki.unity3d.com/index.php/Scripts/Controllers
I had to edit your title, it was disturbing me =]
Am about to sign off, but shall have a look at your script tomorrow. I'm not very good with C# though.
For a better chance at getting an answer, convert your answer to a comment (hopefully it isnt too long), by clicking more > convert to comment . Then your question will show on the main page as having no answer, and hopefully attract someone to help =]
I am the same with my comments, I have to delete them all the time before passing my scripts to my colleague !
Definitely helps when writing/debugging =]
Answer by Beard · Sep 04, 2012 at 09:22 PM
Thank you for the link! I was unable to find a script that suits my needs. I did find another script that I will need, and have not even got to yet. I have been experimenting, and found that if I work with the constraints on an object, I can set the constraints on key press, and the movement will be controlled using the DragRigidBody script. The only problem I am having is that I want the object to have constraints only while the mouse button is down. When I set constraints on mousedown it overrides everything other key press nested within the onmousedown. Each key press should change the constraints, and finally remove them when the item is dropped. (so gravity will act properly on the item) Here is my script. Sorry ahead of time for the amount of commenting, it is how I keep my projects strait so I work on only one thing at a time, and document nested statements.
This script will be placed on each rigidbody that I want to move with itself set as target in the inspector.
using UnityEngine;
using System.Collections;
public class ObjectSelect : MonoBehaviour {
public Transform _target;
// Use this for initialization
void Start () {
}
// Update is called once per frame
#region Update
void Update () {
//Rotate Camera to LEFT side of container
if(Input.GetKeyDown ("q")){
//Will need to check to see what position camera is in.
//if MainCamera Xpos is >= _CameraCenterXPos{
//MainCamera.Transform =
//Else MainCamera.Transform = CenterCameraXPos
// This should act as a TOGGLE, not a push and hold.
// Left side of cube will need renderer.enabled = false
// renderer.enabled = true when left and center.
}
//Rotate Camera to RIGHT side of container
if(Input.GetKeyDown ("e")){
// This key should operate the opposite of the Q button
//
//
// This will act as a camera TOGGLE not a push and hold.
//
//
// Right side of cube will need renderer.enabled = false when on right
//renderer.enabled = true when left and center.
}
if (Input.GetMouseButton(0)) { // if 1
ConstraintsSetZRotate();
if(Input.GetKeyDown("w")){
//Code to control when W is pressed
ConstraintsSetZon();
Debug.Log ("W was pressed");
} //end W KeyDown Code
if(Input.GetKeyUp("w")){
//Code to reset values changed by W
ConstraintsSetZRotate();
Debug.Log ("W was released");
} // end W Keyup code
if(Input.GetKeyDown("left shift")){
//Code to control when Shift is pressed
ConstraintsEnableRotateOnly();
Debug.Log("Left Shift Pressed");
} //end Shift Keydown code
if(Input.GetKeyUp("left shift")){
//Code to reset values changed by Shift
ConstraintsSetZRotate();
Debug.Log("Left Shift Released");
} //end Shift Keyup Code
} // End MouseDown event
if(Input.GetMouseButtonUp(0)){
ConstraintsZonly();
}
} // End Update
#endregion
private void ConstraintsSetZRotate(){
//Sets All Rotation and Z axis to disabled.
_target.rigidbody.constraints = RigidbodyConstraints.FreezeRotation|RigidbodyConstraints.FreezePositionZ;
}
private void ConstraintsSetZon(){
//Disable all positions except Z
_target.rigidbody.constraints = RigidbodyConstraints.FreezePositionX|RigidbodyConstraints.FreezePositionY|RigidbodyConstraints.FreezeRotation;
}
private void ConstraintsEnableRotateOnly(){
//Disable all Positions Enable Only Rotations
_target.rigidbody.constraints = RigidbodyConstraints.FreezePositionZ|RigidbodyConstraints.FreezePositionX|RigidbodyConstraints.FreezePositionY;
}
private void ConstraintsZonly(){
_target.rigidbody.constraints = RigidbodyConstraints.FreezePositionZ;
}
}
Your answer
Follow this Question
Related Questions
C# rotating a 3D object left&right using mousedrag 0 Answers
Rotate on drag for IOS? 1 Answer
LookTo Rotation Mouse Drag 2 Answers
Bullet mouse control 2 Answers
Throwing an object (applying force to it when mouse is released) 2 Answers