0
\$\begingroup\$

enter image description here

Hey guys I've been making a bow and arrow system. I'm stuck on making an arrow facing the right direction before its launch. I've attached 3 arrow prefabs in the right position of the player's hand and set that active status to false. I've just turned set active to true while the particular frame of animation through animation event.it's spawned in the right direction at first. but when my character moves its transform direction is totally changed. I want my arrow is always facing its launch direction. if anyone has an idea about that please tell me the solution are ping any video tuts relative to this. I'm stuck on this long while. here is the code I've scripted to active my arrow prefab.

public void spawnArrow()
{
  
    string name = spawnArrowPool.transform.GetChild(0).gameObject.name;
    Vector3 movdir = new Vector3(0,0,0);
    if(spawnArrowPool.transform.childCount>0)
    {
        
        arrow = name=="Dummy" ? spawnArrowPool.transform.GetChild(1).gameObject : 
        spawnArrowPool.transform.GetChild(0).gameObject;
        arrow.transform.rotation = transform.localRotation;
        arrow.SetActive(true);
    }
}
\$\endgroup\$
2
  • \$\begingroup\$ Do they keep being a child of your parent? Or is the arrow in your screenshot just for visually holding it there and the shot one is located somewhere else? \$\endgroup\$
    – Zibelas
    Feb 7, 2022 at 11:35
  • \$\begingroup\$ On which gameObject exactly do you have that script? Is it on RightHand or on the SpawnArrowPool or on the root player game object? \$\endgroup\$
    – Philipp
    Feb 7, 2022 at 11:56

1 Answer 1

0
\$\begingroup\$

The root of this problem could be some misunderstanding regarding the difference between transform.rotation and transform.localRotation.

I suspect that you have that script on the SpawnArrowPool game object. In that case transform.localRotation refers to the rotation of SpawnArrowPool relative to its parent RightHand.

If you want to spawn objects which are not parented to the same object as the spawner, then you likely want the rotation of the spawner relative to the game world. You can get that rotation with transform.rotation.

If you do want the arrows to stay parented to the SpawnArrowPool, then you don't need to set the rotation at all. You can just do arrow.transform.localRotation = Quaternion.identity. The arrows will then have the same rotation as their parents. But remember that while the arrows are parented to the SpawnArrowPool, they will rotate and move when the arrow pool rotates or moves. Which is the desired behavior for an unlaunched arrow stuck to the hand of the character, but probably not what you want with independent projectiles after they were launched.

For more information on the Transform component, check out the documentation.

\$\endgroup\$

You must log in to answer this question.

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