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

2D Shmup, Instantiate on small angle, no Rigid Body

Discussion in '2D' started by Lambetts, Oct 14, 2019.

  1. Lambetts

    Lambetts

    Joined:
    Mar 4, 2019
    Posts:
    10
    Hi guys,

    First time I've ever posted. I usually search and search and find my answers. Haha.

    This one's got me though.

    2D shooter (bottom up) I'm at a point where I want to have the enemy shoot 3 bullets, I have 3 empty GOs on the front of the enemy S***, one directly down, the other 2 about 10 degrees left and right on the Z axis.

    I've tried using Euler but maybe I'm not using it right. I have a script on the projectile to move in a -Y * speed. It travels down. I just want the prefab to instantiate with the angle, I just can't seem to be able to do it.

    Thank you so much in advance guys.

    Cheers.
     
  2. vakabaka

    vakabaka

    Joined:
    Jul 21, 2014
    Posts:
    1,153
    Code (CSharp):
    1. public GameObject item;
    2.     public void Start()
    3.     {
    4.         GameObject tmpItem = Instantiate(item, transform.position, transform.rotation);
    5.         //rotate the spawned item on z
    6.         tmpItem.transform.eulerAngles = new Vector3(0, 0, -10);
    7.     }
    later you can move the item in its local direction with the Vector2: -transform.up

    other way: you can make rotated empty gameobject and take its rotation for the new spawned object
    Code (CSharp):
    1. Instantiate(item, transform.position, rotatedEmptyGameObject.transform.rotation);
     
    Lambetts likes this.
  3. Lambetts

    Lambetts

    Joined:
    Mar 4, 2019
    Posts:
    10
    Thank you so much for taking the time to respond.

    I gave this a go but it doesnt seem to be going anywhere but straight down.

    I'll show you the code.

    Code (CSharp):
    1. public GameObject enemyGunMountC;
    2.     public GameObject enemyGunMountL;
    3.     public GameObject enemyGunMountR;
    4.  
    5. //Declaration at start of script.
    6.  
    7. void FireEnemyShot()
    8.     {
    9.         GameObject playership = GameObject.FindGameObjectWithTag("Player");
    10.    
    11.         if (playership != null)
    12.         {
    13.             //GameObject enemyshot = (GameObject)Instantiate(enemyShotGO);
    14.             //enemyshot.transform.position = (new Vector3(enemyGunMountC.transform.position.x, enemyGunMountC.transform.position.y, rotation.z));
    15.  
    16.             GameObject enemyshot2 = Instantiate(enemyShotGO, transform.position, transform.rotation);
    17.             //rotate the spawned item on z
    18.             enemyshot2.transform.eulerAngles = new Vector3(0, 0, -10);
    19.  
    20.             Instantiate(enemyShotGO, transform.position, enemyGunMountL.transform.localRotation);
    21.  
    22.  
    23.             //GameObject enemyshot2 = (GameObject)Instantiate(enemyShotGO);
    24.             //enemyshot2.transform.position = enemyGunMountR.transform.position;
    25.         }
    26.  
    27.     }
    Also though, this is EnemyBullet script.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class EnemyLaser : MonoBehaviour
    6. {
    7.     public GameObject laserShotGO;
    8.     public GameObject laserSparkGO;
    9.     public float speed;
    10.  
    11.     private Transform transGunMountL;
    12.  
    13.  
    14.     void Update()
    15.     {
    16.         Quaternion rotation = Quaternion.Euler(0, 0, -10);
    17.  
    18.         Vector2 position = transform.position;
    19.         position = new Vector3(position.x, position.y - speed * Time.deltaTime, rotation.z);
    20.         transform.position = position;
    21.     }
    22.  
    23.     void OnTriggerEnter2D(Collider2D col)
    24.     {
    25.         if (col.tag == "Player")
    26.         {
    27.             GameObject laserShot = (GameObject)Instantiate (laserSparkGO);
    28.             laserShot.transform.position = laserShotGO.transform.position;
    29.  
    30.             Destroy(gameObject);
    31.         }
    32.     }
    33.  
    34. }
    There is some stuff that I have to remove as I've been trouble shooting it haha, But this is the 'Jist'.

    Thank you again.
     
  4. vakabaka

    vakabaka

    Joined:
    Jul 21, 2014
    Posts:
    1,153
    there is no need to rotate the bullet every frame in the bullets script. You have already rotated it by the instantiate.

    with the line:
    position = new Vector3(position.x, position.y - speed * Time.deltaTime, rotation.z);
    the x-position stays the same, the y-position is going smaller and the z-position... (i have no idea, I can't understand quaternions, something like constant and move the bullet on the z-axis). Also the bullet will just go down (+z change but you will not see it in the 2d).
    You can take the local direction from the bullet and move it in this direction (the bullet should be rotated).
    Code (CSharp):
    1. void Update()
    2.     {
    3.         transform.Translate(-transform.up * speed * Time.deltaTime);
    4.     }
    or you can calculate the world vector and move the bullet to it (ok, i wouldn't do this, just an example).
    Code (CSharp):
    1.   Vector2 worldDir = new Vector2(0.1f, -1);
    2.  
    3.     void Update()
    4.     {
    5.         transform.Translate(worldDir * speed * Time.deltaTime);
    6.     }
     
    Last edited: Oct 15, 2019
    Lambetts likes this.
  5. Lambetts

    Lambetts

    Joined:
    Mar 4, 2019
    Posts:
    10
    Wow, thanks man!!! I put that simple line on the bullet, and it is now translating the angle from the parent object!!

    I've been wondering how to do this, Now I can fire prefabs in any direction based on the parent object yes?

    Can you explain to me how that is doing this?

    Thank you thank you!!!!!!!!!!
     
    Last edited: Oct 16, 2019
  6. vakabaka

    vakabaka

    Joined:
    Jul 21, 2014
    Posts:
    1,153
    this is not the angle from the parent object. transform.up (or transform.right) moves the object straight up (right) but in its own local transform

    local.png
    transform.Translate (vector2(3)) is just move the object in one frame in the direction from the vector and the distance of the moving will be the length from the vector.
     
  7. Lambetts

    Lambetts

    Joined:
    Mar 4, 2019
    Posts:
    10
    Oh ok. Gee, thanks so much again for this, that's a great explanation (and detailed) and I hope others get something out of it.

    Still though, I change the parent (like a gun mount of the ship called, something like gunMountL) z to - 10, and it the instantiated prefab travels on that angle. Which is great but, I'd just like to know how the hell it does that haha. I'm thinking the translate function has something to do with it. Sorry, I'm very new to c# and unity. What I'm saying is probably so dumb haha.

    Thank you! ☺