Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Resolved Bullets only going in one direction no matter the rotation of the player

Discussion in 'Scripting' started by Niciswelt, Feb 1, 2023.

  1. Niciswelt

    Niciswelt

    Joined:
    Feb 1, 2023
    Posts:
    4
    Hey. In the 2D space shooter of my project group we have a spaceship that can turn +-30° depending on the mouse position. The problem is its Bullets will only go straight on the x-Axis and don't turn with the ship. How can I make them shoot in the direction that the ship is turned? We've tried many fixes but can't seem to make it work.

    Shooting aspect basically:

    Code (csharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5.  
    6. public class Entity_bullets : MonoBehaviour
    7. {
    8.     public bullet_Behavior bulletPrefab;
    9.     public Transform BulletSpawnPoint;
    10.  
    11.     public float Damage = 10f;
    12.     public float Cooldown = 0.1f;
    13.     private float Timer = 0.1f;
    14.  
    15.  
    16.     public string Enemy;       //Only give value when given to Enemy
    17.  
    18.     void Update()
    19.     {
    20.        
    21.         if(Timer > 0)
    22.         {
    23.             Timer -= Time.deltaTime;
    24.         }
    25.         if(Input.GetKey(KeyCode.Space) && Timer <= 0 && gameObject.tag == "Player")
    26.         {
    27.             bullet_Behavior Bullet = Instantiate(bulletPrefab, BulletSpawnPoint.position, transform.rotation);
    28.             Rigidbody2D BRBody = Bullet.GetComponent<Rigidbody2D>();
    29.             BRBody.velocity = new Vector2(15, 0); // BECAUSE WE ARE TO STUPID
    30.             Bullet.Damage = Damage;
    31.             Timer = Cooldown;
    32.         }
    33.     if(Timer <= 0 && gameObject.tag == $"{Enemy}"){
    34.        
    35.         bullet_Behavior Bullet = Instantiate(bulletPrefab, BulletSpawnPoint.position, transform.rotation);
    36.             Rigidbody2D BRBody = Bullet.GetComponent<Rigidbody2D>();
    37.             BRBody.velocity = new Vector2(-15, 0);
    38.             Bullet.Damage = Damage;
    39.             Timer = Cooldown;
    40.     }
    41.    
    42.    
    43.     }
    44. }
    45.  
    46.  
    Bullets basically:

    Code (csharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5.  
    6. public class bullet_Behavior : MonoBehaviour
    7. {
    8.    
    9.     [HideInInspector]
    10.     public float Damage;
    11.  
    12.    
    13.     private Rigidbody2D rBody;
    14.     private void Start()
    15.     {
    16.         rBody = GetComponent<Rigidbody2D>();
    17.         //rBody.velocity = new Vector2(speed, 0);
    18.     }
    19.     void Update()
    20.     {
    21.        // transform.position += transform.right * Time.deltaTime * speed;  
    22.     }
    23.     private void OnTriggerEnter2D(Collider2D collision)
    24.     {
    25.         HealthComponent component = collision.GetComponent<HealthComponent>();
    26.         if (component != null)
    27.         {
    28.             Debug.Log(collision.gameObject.name);
    29.             Destroy(gameObject);
    30.             component.HealthSystem.Damage(Damage);
    31.         }
    32.     }
    33. }
    34.  
     
    Last edited: Feb 1, 2023
  2. angrypenguin

    angrypenguin

    Joined:
    Dec 29, 2011
    Posts:
    15,519
    We're not going to download your source to take a look. Put it in here in < code > tags so we can see it directly and someone might be able to spot your error.

    This is a guess: I assume that you have a spawn point which is a child of your ship. Are you making sure to transform the orientation and velocity of bullets from local space to world space when you spawn them? There are methods on the Transform which will help with this.
     
  3. Niciswelt

    Niciswelt

    Joined:
    Feb 1, 2023
    Posts:
    4
    Yup, added tags, thanks for telling me.
    Do you have an example for the world spawn?
     
    angrypenguin likes this.
  4. angrypenguin

    angrypenguin

    Joined:
    Dec 29, 2011
    Posts:
    15,519
    You're already spawning in world space, so that's fine.

    The issue is lines 29 and 37, where you're giving fixed velocities. The velocity will always be (+/- 15, 0) because that's exactly what you're telling it to do. If you want the velocity to be based on where the ship is pointing then you need to use the ship's forward direction when setting bullet velocity.

    Check out the documentation for the Transform class, it has some properties which will definitely help you.
     
  5. Niciswelt

    Niciswelt

    Joined:
    Feb 1, 2023
    Posts:
    4
    I don't think that would work because we already tried random velocity values and that didn't change anything except the speed on the x-Axis.
     
  6. SF_FrankvHoof

    SF_FrankvHoof

    Joined:
    Apr 1, 2022
    Posts:
    780
    Velocity is a vector in 2D/3D (world-)Space, so I'm pretty sure it would have changed more than just the speed on the x-axis, unless you only adjusted the x-axis of the velocity.

    If you want to move forward in a speed, what you're looking for is
    rb.velocity = transform.forward * speed;


    (In the case of 2D games this might be transform.right * speed, or transform.up * speed, depending on which direction would be considered 'forward' for your object.)
     
    angrypenguin likes this.
  7. Niciswelt

    Niciswelt

    Joined:
    Feb 1, 2023
    Posts:
    4
    Okay we found it after another hour of bugfixing. Somebody accidentally locked the y-Axis of the bullets. Welp. But it works now so still thanks for helping.