- Home /
Mouse locked in center
I have a script that allows me to pick up objects when my mouse is over the object. The only problem is, the mouse is constantly drifting to a side. I created a simple cursor lock script, but it only made the cursor invisible. Which script reference would make it so the cursor locked in place, almost like a crosshair? I got rid of the script that made the cursor invisible, but I have the pick up script here
`private var pickObj: Transform = null; private var hit: RaycastHit; private var dist: float; private var newPos: Vector3;
function Update(){
if (Input.GetMouseButton(0)){ // if left button creates a ray from the mouse
var ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if (!pickObj){ // if nothing picked yet...
if (Physics.Raycast(ray, hit) && hit.transform.tag == "Pick"){
// if it's a rigidbody, zero its physics velocity
if (hit.rigidbody) hit.rigidbody.velocity = Vector3.zero;
pickObj = hit.transform; // now there's an object picked
// remember its distance from the camera
dist = Vector3.Distance(pickObj.position, Camera.main.transform.position);
}
}
else { // if object already picked...
newPos = ray.GetPoint(dist); // transport the object
pickObj.position = newPos; // to the mouse position
}
}
else { // when button released free pickObj
pickObj = null;
}
}
Answer by Kyu · Jul 18, 2012 at 09:36 PM
I've done this recently, basically used Screen.lockCursor, which hides the mouse pointer, but also locks it to the center of the screen. Then you have to replace it with something, so I added a GUITexture with a crosshair-like texture, and just center it on the screen.
The code looks something like this:
void Start () { Screen.lockCursor = true; }
And the GUITexture is positioned at (0.5, 0.5, 0.0), locking it on the center of the screen.
Your answer
Follow this Question
Related Questions
Screen.lockCursor not working? 3 Answers
turn off script with a key.. 1 Answer
Object pick up script 1 Answer
Mouse Aiming Follow Wrong Direction - Help 2 Answers
Unity 3D Screen.lockCursor Problems 2 Answers