Search Unity

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

Question Instantiate object going sideways

Discussion in 'Getting Started' started by SmaugAlert, Oct 28, 2023.

  1. SmaugAlert

    SmaugAlert

    Joined:
    Sep 2, 2023
    Posts:
    3
    I have an enemy that will go towards the player and once in attack range, throw a grenade every 2 seconds.
    The object is instantiated at the enemy's position and rotation, and the force is applied in the enemy's forward direction. The enemy also looks at the player right before this. However, the enemy's grenade goes sideways. I have changed the addforce to enemything.transform.right, but then the grenade actually goes right, and I have verified that the enemy is facing towards the player. The grenade prefab also has 0, 0, 0 rotation values.



    Code (CSharp):
    1.  
    2. transform.LookAt(player);
    3. GameObject clone = Instantiate(grenade, enemything.transform.position, enemything.transform.rotation);
    4.  
    5. clone.GetComponent<Rigidbody>().AddForce(enemything.transform.forward * 800);
    6.  
    7.  
    8.  
     
  2. halley

    halley

    Joined:
    Aug 26, 2013
    Posts:
    2,277
    The forward vectors is the correct approach. Your prefab may be rotated, or the children of the prefab root may be rotated. Edit the prefab in isolation, and toggle the "local space, world space" mode; the blue arrow should face the same way in either mode, and the models aligned properly for that forward direction.
     
  3. SmaugAlert

    SmaugAlert

    Joined:
    Sep 2, 2023
    Posts:
    3
    So I have figured out that the enemy and grenade prefab are both faced in the correct direction. When I removed the line of rigidbody addforce, the grenade dropped directly under the enemy. So the issue is in the line of code
    "clone.GetComponent<Rigidbody>().AddForce(enemything.transform.forward * 800);"
    but I have paused the scene as the enemy dropped the grenade, and it goes directly right. I then changed the "transform.forward" to "-transform.right" but this just makes the enemy throw them diagonally behind it. The enemy's blue z axis arrow still always points towards the player, but the grenade's points toward the enemy. This made me try changing the "transform.forward" to "-transform.forward" but this makes the enemy throw them diagonally behind it again. I have figured out that standing directly in front of the enemy makes the grenade drop down with little force, and when I go to the left, the grenade is thrown to the enemy's right, and when I go to the right, the grenade is dropped to the enemy's left. So the grenade is thrown in a line parallel to the player which confuses me, because the grenade prefab rotation is normal, and the enemy's rotation is normal, but the instantiated prefab's is not. I can't fix the issue, but I know it is an Issue with the rigidbody addforce line, and in the instantiated prefab. The grenade script also has no references to rotation or transform at all, just exploding and etc.