- Home /
Adjusting Rotation of Instantiated Objects
I have a sword in a game. When you click on the sword, it deletes and creates another one in your hand. It does this correctly but with a random rotation. How do I fix this?
#pragma strict
var hand : GameObject;
var sword : Transform;
function OnMouseUp()
{
Destroy(gameObject);
var clone = Instantiate(sword, hand.transform.position, Quaternion.identity);
clone.transform.parent = hand.transform;
Debug.Log("The Sword is in your Inventory");
WaitForSeconds(1);
}
Is just a lot more easier to already have it there and just set to invisible ..ins$$anonymous$$d of instantiating others. It doesn't impact the performance a lot neither (unless you have thousands of swords ).
I agree with @Crystalline that having the sword in place and just making it visible is a nice way to go. If you want to handle it your way:
In the editor temporarily place a sword in the hand correctly.
$$anonymous$$ake the sword a child of the hand.
Record the rotation in the Inspector. This is the local rotation...that is the rotation relative to the parent.
Between lines 17 and 18 insert:
clone.transform.localRotation = Quaternion.Euler([Insert recorded values here]);