Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Spawning an object has object original rotation for a split second

Discussion in 'Scripting' started by Robstao, Apr 15, 2020.

  1. Robstao

    Robstao

    Joined:
    Jan 23, 2019
    Posts:
    11
    I have an issue where I shoot an arrow from a bow, the arrow being a prefab.

    Once I shoot the bow, I instantiate the arrow object and add force to it, like so:

    Code (CSharp):
    1. public void Shoot(float force, float playerRangedDamage) {
    2.         GameObject projectile = Instantiate(projectileObject, projectileSpawnPoint.transform.position, projectileSpawnPoint.transform.rotation);
    3.         projectile.GetComponent<Arrow>().SetDamage(playerRangedDamage);
    4.         projectile.GetComponent<Rigidbody>().AddForce(projectileSpawnPoint.transform.forward * force);
    5.     }
    Works great! But sometimes, when I instantiate the object I can see the arrow for a split second being spawn in the prefab original rotation (least I think that that is what it is?) and THEN it rotates straight and shoots.

    It's fairly constant, perhaps 1 in every 4 shots show I can see the arrow sideways, especially when there isn't a lot of force. If the force is really high it isn't noticeable at all.

    I've been tinkering with solutions (disable the mesh renders of the arrow then enable again) in hopes that I can almost skip that initial spawning rotation, but no cigar.

    Any suggestions?
     
  2. Owen-Reynolds

    Owen-Reynolds

    Joined:
    Feb 15, 2012
    Posts:
    1,989
    Rotation changes shouldn't have any sort of delay, and objects don't rotate by themselves based on speed (unless they have joints). It's probably something else. Is the Arrow script also changing the rotation (some aim it towards the direction of firing, to arc like an arrow)? Is the spawn point aimed with +z facing forward? Is the prefab aimed that way? Is the "gun" rotated after shooting, and can it be by a large amount?

    For tests, could try disabling the Arrow script in the prefab. Could set the prefab's rotation to something easy, like straight up, then fire sideways. See if there's a pattern depending on which way you're aimed. Try changing the prefab into something really obvious so you can spot the bad aim better.