Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice

Question Align Instantiated prefab to hand?

Discussion in 'Physics' started by GohanCZ, May 15, 2021.

  1. GohanCZ

    GohanCZ

    Joined:
    Apr 29, 2021
    Posts:
    32
    Hello,

    I have a prefab sword, oriented along the Z axis(the tip pointing forward).

    I have a hand, that has its transform, including the position and rotation.

    I want to Instantiate the sword in a position where it is "held" by the hand.

    Instantiating as Quaternion.identity always spawns it in its default orientation. I also tried working with Quaternion.Euler adding and subtracting, but that produced very weird results, where in one position the sword spawned perfectly aligned, but if I turned the sword would spawn way off. Almost as if some of the Euler angles capped at 180 but the Instantiation needed a 360..not sure how to explain.

    Would woul be the best way to align the Instantiate to a game object that moves around the scene, including the rotation?

    Thank you
     
  2. We usually do this by attaching an empty game object (attachment point) to the palm, with the Z (forward) pointing out between the index and the thumb, parallel with the palm. Add an empty game object to the weapon too, where it should meet with the attachment point and rotate it in a similar fashion. Rotate towards where the weapon should face between the index and the thumb parallel with the palm.

    Now, you can simply make the weapon's attachment point a child of the palm's attachment point and if you did everything right, the weapon should self-orientate itself if all the rotations are zero.

    This isn't really a physics questions.
     
  3. GohanCZ

    GohanCZ

    Joined:
    Apr 29, 2021
    Posts:
    32
    This is a bit complicated, because the models for the hands get instantiated when the scene starts, they are not visible in the editor.

    The weapon itself already has an attachment point, which is a spot where the hand holds the item and that is set up correctly.

    The issue is when I instantiate the weapon it is not oriented correctly.

    Part of the issue might also be that the "origin" of the weapon is at the base of the hilt, therefore it is rotating off a funky axis, which is probably why the Euler was throwing me off.

    I also believed this to be a question of angles and since there is no Math subforum I picked physics as the next best fit.
     
    Last edited: May 15, 2021