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

Bullets Fly Up?

Discussion in 'Scripting' started by Cronoss95, Aug 16, 2020.

  1. Cronoss95

    Cronoss95

    Joined:
    Apr 24, 2019
    Posts:
    13
    Hello!

    I was following this tut:


    but my bullets are flying up, instead straight from the gun. I tried multiple things that were suggested in the comments (vector2.up, detach firepoint etc.) but nothing works. Can someone tell me what is wrong with my code? I have:

    - the sprite is pointing up, like in the tud

    - I put the codes at the appropriate places

    - I also checked the tuts man gitHub, the code there wont work either.



    If somebody could help me out here, I would be really happy! Thanks! :)
    I posted the same thing here, with pictures : https://www.reddit.com/r/C_Programming/comments/ib000f/bullets_fly_up/
     
  2. Terraya

    Terraya

    Joined:
    Mar 8, 2018
    Posts:
    646
    im mostly focusing on 3D Games,
    but as i can see in your code you got:

    Code (CSharp):
    1. Vector2.up
    which causes the problem most probably,

    you would need to use the vector your looking at, in your case that would be Left/Right i think :)
     
  3. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,888
    You want to use
    transform.right
    instead of
    Vector2.up
    in your Translate call.
     
    Terraya likes this.
  4. Cronoss95

    Cronoss95

    Joined:
    Apr 24, 2019
    Posts:
    13
    Is there any way to create a vector in the direction the mouse is pointing? because transform.right is good for looking right, but when you look left, the bullet flies to the right, through my player.
    But I have a script, that I use to move the weapon around, is there a way to use it for the bullet?
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Gun : MonoBehaviour
    6. {
    7.     //Was brauchen wir? Die Waffe soll dem Mauszeiger folgen. Wie geht das mit Skripten? Wir müssen den WINKEL berechnen, in dem der Sprite dem Cursor folgt.
    8.     public float offset;
    9.  
    10.  
    11.     //Nun, da wir die Waffe drehen können, brauchen wir das eigentliche Schießen
    12.     public GameObject bullet;
    13.     //Die Bullet muss natürlich von irgendwoher kommen...
    14.     public Transform firePoint;
    15.  
    16.     // cCountdown, bevor man wieder schießen kann.
    17.  
    18.     private float timeBtwShots;
    19.     public float startTimeBtwShots;
    20.  
    21.  
    22.  
    23.     private void Update()
    24.     {
    25.         //Zu erst müssen wir den allgemeinen Abstand zwischen Cursor und Sprite berechnen.
    26.         Vector3 difference = Camera.main.ScreenToWorldPoint(Input.mousePosition) - transform.position; //direction = destination - origin
    27.         float rotZ = Mathf.Atan2(difference.y, difference.x) * Mathf.Rad2Deg; //das berechnet den Abstand
    28.         transform.rotation = Quaternion.Euler(0f, 0f, rotZ + offset);
    29.  
    30.  
    31.         if (timeBtwShots <= 0)
    32.         {
    33.             if (Input.GetMouseButtonDown(0))
    34.             {
    35.                 Instantiate(bullet, firePoint.position, transform.rotation);
    36.                 timeBtwShots = startTimeBtwShots;
    37.             }
    38.         }
    39.  
    40.         else
    41.         {
    42.             timeBtwShots -= Time.deltaTime;
    43.         }
    44.  
    45.         //Die Bullets sind sehr einfach. Das Public GameObject ist unser Projektil, und wenn wir eine Taste drücken, wird dieses gespawnt
    46.     }
    47. }
    48.  


    and this script is on the bullet
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Bullets : MonoBehaviour
    6. {
    7.     public float speed; //Wie schnell ist die Kugel?
    8.     //Die Kugeln sollen natürlich kaputt gehen, wenn sie nicht auf Geister treffen
    9.  
    10.     private void Update()
    11.     {
    12.         transform.Translate(Vector3.forward * speed * Time.deltaTime);
    13.     }
    14.  
    15. }
    16.