2
\$\begingroup\$

Implementation question from a beginner. I would like to implement a functionality to a first person game where the player can pick up notes lying around and have the note align itself to the camera and get bigger for the player to read its contents. The game will have a Myst like feel to it.

enter image description here

I am currently debating whether I should use the UI or just animate the note object in the game world itself and have it "fly" towards the camera.

So far I've implemented a solution which uses a raycast and when the ray hits an item it sends a message to it. If the item can understand this message, then it looks at the camera (using lookAt()) and becomes a child of the camera, I then reposition it in front of the camera using the localPosition.

All this seems to work fine but my object is still part of the world, I would ideally want it to be unable to collide with objects when it is carried around by the player. My plan was then to have a couple of UI button pop-up each time an item is picked up for the player to either drop it or stash it in its inventory.

Any suggestions ?

Thank you

\$\endgroup\$
6
  • \$\begingroup\$ Have you tried either disabling the collider upon receiving the message, or, if you're using a Rigidbody, making it kinematic? \$\endgroup\$
    – zcabjro
    Commented Mar 14, 2017 at 12:02
  • \$\begingroup\$ I'm unclear as to what the "is kinematic" does to a Rigidbody, I noticed it appears static when it is, but would it also prevent collisions ? \$\endgroup\$
    – Sorade
    Commented Mar 14, 2017 at 12:05
  • \$\begingroup\$ Your best bet is to disable the note's collider (no collider, no collision). But if your note is normally affected by gravity, it is common to make it kinematic before assuming control of it via script. It will no longer be affected by physics this way. docs.unity3d.com/ScriptReference/Rigidbody-isKinematic.html \$\endgroup\$
    – zcabjro
    Commented Mar 14, 2017 at 12:12
  • \$\begingroup\$ Okay, thanks I will try this tonight. When it comes to handling the item inside the inventory rather than the world, would I be best to have a parent game object which contains both a "world item" and an "iventory item" and just disable the "world item" child when the item is in the inventory. \$\endgroup\$
    – Sorade
    Commented Mar 14, 2017 at 12:16
  • \$\begingroup\$ Is your inventory displayed in world space? Regardless, I imagine you could accomplish this in many different ways. I would suggest making a separate question if you aren't able to find answers from these questions: gamedev.stackexchange.com/search?q=inventory \$\endgroup\$
    – zcabjro
    Commented Mar 14, 2017 at 12:20

1 Answer 1

3
\$\begingroup\$

To expand a bit on the comments with some actual code, this is how you might add to what you already have. By disabling the Collider, you avoid unwanted collisions. By setting isKinematic to true, you avoid unnecessary physics calculations, as well as any conflicts between your scripted movement and the Rigidbody.

private Rigidbody body;
private Collider col;

private void Awake()
{
    // Assuming a simple, flat hierarchy
    body = GetComponent<Rigidbody>();
    col = GetComponent<Collider>();
}

private void MessageReceived()
{
    SetCollisionsActive(false);

    // Your camera related logic here
}

private void SetCollisionsActive(bool active)
{
    body.isKinematic = !active;
    col.enabled = active;
}
\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .