picking up objects & locking the mouse to center

Hello everyone,
I’m a 2nd Semester student and my professor and I have been struggling with this for a while now, so I thought I’d try the hive mind.

My first problem is that I have a simple script to pick up an object, make it hover in front of the player’s face for as long as the mousebutton is pressed and delete it when the button is let go of. Only for some reason, the object flies into oblivion as soon as I pick it up. We turned off the Destroy gameobject for testing and it really just keeps flying.

This is the only code that is used for the picking up:

public class pickup : MonoBehaviour
{
   public Transform theDest;
   public results mySave;

   public Flowchart fc;

   void OnMouseDown()
   {
       GetComponent<Rigidbody>().useGravity = false;
       this.transform.parent = theDest;
       this.transform.localPosition = new Vector3(0,0,0);
   
       BooleanVariable va = fc.GetVariable<BooleanVariable>("evidenceBag");
       va.Value = true;
   }

   void OnMouseUp()
   {
       //this.transform.parent = null;
       //GetComponent<Rigidbody>().useGravity = true;
       mySave.hasBag = true;
       //Destroy(gameObject);
   }
}

The other issue, that is kinda related, is that the mouse being locked for a first person game doesnt get locked to the centre of the screen?
I have tried both a controller from the asset store and a self-made controller but in both cases, the mouse gets locked somewhere to the top left of the screen. So it makes it super hard to actually click on an object.

Does anyone know a way to lock it to the actual centre?
Ideally something that can be turned on and off in a separate script, so I can use the mouse to pick Fungus dialogue options later in the game.

Would really appreciate if anyone could help me out, picking up stuff and doing dialogue is pretty much the only function my project this semester has so I cant really do any work on the game while that is still broken.

Hi,

Do the player picking up the object, also have a rigid body?
Then it´s probably better to destroy the Rigidbody component instead of just disabling the useGravity.

Not sure what you mean with locking the mouse to the center of the screen.

To pick an object:-

· Your object can follow mouse position on click event, it will act as User is picking object.

· To follow mouse cursor position you can use “this.transform.position = Input.mousePosition;” in Update function (in case your Object is in 2D).

· If your object exists in 3D you need to convert coordinates from 2d to 3d space, for that, you can use this code- Camera.main.ScreenToWorldPoint(Input.mousePosition);

To lock the mouse to center:-

As per our initial understanding we feel that you are looking for some piece of code by which you can lock/unlock the cursor movement on the screen. Here is some similar solution that we found working fine.

· These are a various modes that control the behavior of the Cursor. A default cursor must be set in PlayerSettings > Default Cursor (have a look into the code snippet attached with this mail).

· Cursor.lockState moves the pointer to the center of the screen.

· You could also use a MouseMove event for example.

· Not very clean but it does the job, you retain OnMouseDown functionality, the cursor remains visible.

5911862–631241–LockCursor.cs (863 Bytes)

How do I remove the Rigidbody Component on click?
I tried removing it in general but then the object just floats in the air instead of following the camera.

I used Cursor.lockState so I can use the mouse to look around but the cursor doesnt get locked to the center of the screen, so if I want to pick something up I have to kinda guess where it is somewhere on the top left of the screen.

The Cursor.lockState is what I used, following a YouTube tutorial, but it doesnt lock it to the centre for some reason.
It is somewhere on the top left.

Perhaps there was an error when the Rigidbody component was destroyed, exiting the code, and preventing the parenting?

Did you check the logs?

Console says “Object reference not set to an instance of an object” now

Is the error in line 10?
If so, the rest of the code will be skipped, and the object parent won’t be changed.

Can you please share your code snippet for cursor lock functionality.

public class mouselook : MonoBehaviour
{
    public float mouseSensitivity = 100f;

    public Transform playerBody;

    private float xRotation = 0f;
   
    // Start is called before the first frame update
    void Start()
    {
        Cursor.lockState = CursorLockMode.Locked;
    }


    // Update is called once per frame
    void Update()
    {
        float mouseX = Input.GetAxis("Mouse X") * mouseSensitivity * Time.deltaTime;
        float mousey = Input.GetAxis("Mouse Y") * mouseSensitivity * Time.deltaTime;

        xRotation -= mousey;
        xRotation = Mathf.Clamp(xRotation, -90f, 90f);

        transform.localRotation = Quaternion.Euler(xRotation, 0f, 0f );
        playerBody.Rotate(Vector3.up * mouseX);
    }
}

For some reason removing the destination and then re-adding it did the trick, the picking up seems to work perfectly now!
Thanks so much for the help!

1 Like