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

Instantiating makes wonky rotations

Discussion in 'Editor & General Support' started by Nackles42, Aug 8, 2018.

  1. Nackles42

    Nackles42

    Joined:
    Feb 8, 2018
    Posts:
    6
    Hi all, I'm trying to create a 2D space shooter, and in this game, the ship obviously needs to be able to, well, shoot. The code I'm using to Instantiate my projectile is:
    Instantiate (special, transform.position, transform.rotation);

    "special" is the name of the object I'm trying to instantiate. I also have, attached to the "special" object, a script that tells it to move forward (transform.up). This works fine, however, upon launching the projectile, it sometimes shoots in the right direction and sometimes doesn't. When I'm looking up, it fires up, when I'm looking down, it fires up, when I'm looking left or right, it fires down. It looks like it's treating a 180* turn as a 360* turn, and I don't know how to fix it. Any advice?
     
  2. Nackles42

    Nackles42

    Joined:
    Feb 8, 2018
    Posts:
    6
    I have tried testing other things, however none seem to work. Most of my attempts simply make the object always spawn in one place, regardless of where I shoot from, the bullet spawns at the same spot and goes in the same direction. I am unsure of where to go or what to try next to fix this. Any help would be much appreciated!
     
  3. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    Your instantiate line should place the object at the same position and rotation as the object which called the script. The instantiate line though doesn't have anything directly to do with which direction a projectile is "launched". There could be a problem in your code where you apply velocity or a force. You could be having a collider physics interaction between the projectile and the object you're instantiating it on top of which is sending it in an unexpected direction.
     
  4. Nackles42

    Nackles42

    Joined:
    Feb 8, 2018
    Posts:
    6
    So, I believe I found the root of the problem, but I don't know how to solve it. How I have this working currently is that I have the projectile as a prefab, and within the prefab's script, I have it access the player's rotation so it knows which way to fire. However, because it won't let me set the object in my scene as the transform, I made a prefab of my player to put into the variable. I believe what's happening is that it's trying to use the variables for position and rotation based on the prefab, leading it to always spawn in one place and face the same direction. Does anyone know how to make it go based off of the object in the scene?
     
  5. greg-harding

    greg-harding

    Joined:
    Apr 11, 2013
    Posts:
    523
    After you instantiate the projectile prefab in the scene, you'll need to tell it where the player is in the scene. There are a number of ways to approach this, eg. by passing the player reference into some projectile initialisation method after you instantiate it, by parenting some of your transforms and letting them figure it out based on hierarchy, by the projectile finding the player when it spawns, or perhaps some other method (scene/app controllers that know about your important objects, dependency injection, etc.).

    The important part you've figured out is that prefabs as assets cannot hold references to things in a scene. The way you describe it, there are two player objects - one in the scene, and one prefab in the project resources. These are two different objects which is why you're having trouble.
     
  6. Nackles42

    Nackles42

    Joined:
    Feb 8, 2018
    Posts:
    6
    I see. Thank you for your help, and I will keep posted for if it works or doesn't work. I'm not super new to Unity, but also not super experienced. Having people to ask about things has proven to be quite helpful.
     
  7. Nackles42

    Nackles42

    Joined:
    Feb 8, 2018
    Posts:
    6
    Alright, well it seems to finally be going off of something, but it still just isn't shooting in the direction I'm facing. I'm going to be attaching a couple things to help illustrate what I mean, because this is quite bizarre to me.
    idonotunderstand.png
    Again, this does not make any sense to me, the related code is in the video. Any help is much appreciated. Thank you.
    Also, the object not spawning at my location isn't as big of an issue, I figured out how to fix that one since making the video. The rotation is still what eludes me.
     
  8. greg-harding

    greg-harding

    Joined:
    Apr 11, 2013
    Posts:
    523
    Double-check your coordinate systems. If your player uses z as 'forward' but your projectile uses y as 'forward', then things won't match up and you'll have to transform the rotation to accommodate it.

    Also, are you intending the projectiles to turn/rotate with the player, or just move in their initial direction? Your code currently updates the projectile every frame which is why it curves when the player rotates.
     
  9. Nackles42

    Nackles42

    Joined:
    Feb 8, 2018
    Posts:
    6
    No, I did not intend for the projectile to curve with the player. Thanks.

    Also, from what I can tell, both the player and the projectile use y as 'up' and I've been using 'transform.up' for both the player and projectile's movement. Would that work or should I modify stuff so it uses 'transform.forward'?
     
  10. greg-harding

    greg-harding

    Joined:
    Apr 11, 2013
    Posts:
    523
    You can use whatever works, so long as you're aware of how to translate coordinate systems when you need.

    From your diagram, it looks like there could be a transform nesting problem, where the projectile rotation is being added to the player rotation because it's a child of something else being rotated. Check your hierarchy for where the player and projectiles are, and adjust your code to compensate or reparent something somewhere else. Your code is using global transform components but that doesn't mean that nested transforms isn't upsetting things.

    Use some debug tools like Debug.DrawLine to help figure things out. Draw lines pointing in the various directions you're trying to work with to see if they match what you think is going on.
     
  11. Taurunti

    Taurunti

    Joined:
    Aug 20, 2019
    Posts:
    1
    I found this post because I had the exact same issue (I am also doing a space shooter game, what are the chances!) and I was super disappointed that nobody had a solution here!

    Eventually I discovered the source of the issue: for some reason I don't understand, using "transform.right" to get a vector for movement behaves in this way. To get the CORRECT behavior (moving in-line with rotation/orientation) you will instead want to use "Vector3.right".

    For example,
    Code (CSharp):
    1. // DO NOT do this
    2. transform.Translate(Speed * Time.deltaTime * transform.right);
    3.  
    4. // Instead, do this
    5. transform.Translate(Speed * Time.deltaTime * Vector3.right);
    Answer was found in this other thread: https://forum.unity.com/threads/rotation-is-doubled-on-object.1123678/
     
    emris16 likes this.