Search Unity

  1. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Resolved Set Parent Changing Position of Parent

Discussion in 'Scripting' started by Topfall, Nov 11, 2023.

  1. Topfall


    Dec 31, 2020
    I have a script that is a sort of inventory system in 3d using a list and positioning of objects.

    Code (CSharp):
    1. void Drop(GameObject objectDrop)
    2. {
    3.     RaycastHit hit;
    4.     if (Physics.Raycast(cam.transform.position, cam.transform.forward, out hit, raycastDistance))
    5.     {
    6.         int index = Objects.IndexOf(objectDrop);
    7.         objectDrop.transform.position = hit.point;
    8.         objectDrop.transform.rotation = Quaternion.identity;
    9.         objectDrop.transform.parent = null;
    10.         Objects.Remove(objectDrop);
    11.         SetImage(index, Sprites[2]);
    12.     }
    13. }
    This code is for dropping the object you are holding. The makes a raycast and places the object where it hits. When the object is in the players possession it has a parent of "Hand". When dropping the object the parent is set to null for the scene. When setting the parent of the object to "null" the player character changes position by a small amount. I am not sure why this is happening, Thanks to anyone who helps. 11-10-2023.gif
  2. zulo3d


    Feb 18, 2023
    When you place the disc in your hand then it's probably intersecting your character. Try disabling the collider on the disc before placing it in your hand. Or don't place it so close to the character.