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

Bullet doesnt have the proper rotation

Discussion in 'Scripting' started by kancelarija12, Feb 7, 2020.

  1. kancelarija12

    kancelarija12

    Joined:
    Sep 17, 2019
    Posts:
    15
    So when i click a button that shoots(instantiates a bullet) it should be at rotation and position of the player but it isnt rotated properly and it goes in a random direction. I have made them not collide because i thought it was because of that but it wasnt.

    playerscript:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Player : MonoBehaviour
    6. {
    7.     public GameObject bullet;
    8.     public GameObject a;
    9.     public Transform rok;
    10.     public Rigidbody2D rb;
    11.     public float was = 2;
    12.     public bool g = false;
    13.     public bool r = true;
    14.     public float speed;
    15.  
    16.     public AudioSource SS;
    17.  
    18.     public void go()
    19.     {
    20.         r = false;
    21.         g = true;
    22.     }
    23.     public void notgo()
    24.     {
    25.         r = true;
    26.         g = false;
    27.     }
    28.     public void shoot()
    29.     {
    30.         r = false;
    31.         SS.Play();
    32.         GameObject bu = Instantiate(bullet);
    33.     }
    34.     void Start()
    35.     {
    36.         rb = a.GetComponent<Rigidbody2D>();
    37.     }
    38.  
    39.     void Update()
    40.     {
    41.  
    42.         if(g == true)
    43.         {
    44.             a.transform.Translate(transform.up * speed * Time.deltaTime);
    45.         }
    46.         else
    47.         {
    48.             rb.velocity = Vector2.zero;
    49.         }
    50.         if(r == true)
    51.         {
    52.             a.transform.Rotate(0, 0, 50 * Time.deltaTime);
    53.         }
    54.         if(r == false)
    55.         {
    56.             if(g == false)
    57.             {
    58.                 was -= 1 * Time.deltaTime;
    59.             }
    60.         }
    61.         if(was < 1)
    62.         {
    63.             was = 2;
    64.             r = true;
    65.         }
    66.     }
    67. }
    68.  
    And the bullet script:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Bullet : MonoBehaviour
    6. {
    7.     public GameObject pt;
    8.     void Start()
    9.     {
    10.         pt = GameObject.Find("Rocket");
    11.         transform.position = pt.transform.position;                                                            
    12.         transform.rotation = pt.transform.rotation;
    13.     }
    14.     void Update()
    15.     {
    16.         transform.Translate(transform.up * 3 * Time.deltaTime);
    17.     }
    18.     void OnCollisionEnter2D(Collision2D collision)
    19.     {
    20.         if (collision.gameObject.tag == "Enemy")
    21.         {
    22.             Destroy(this.gameObject, 0.1f);
    23.         }
    24.     }
    25. }
     
  2. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    In Bullet.Start() you are aligning the bullet with the rotation of whatever "Rocket" is. If the rotation is incorrect then either you don't have the bullet's model aligned properly with its own Z axis (making it just appear to be incorrect even though it is correct), or the rotation of Rocket isn't what you expect it to be (more likely I'd guess).

    After that it appears you move the bullet "up" along its own Y axis. But if the bullet isn't facing the direction you want it to, then movement relative to its rotation like this can of course be in the wrong direction. So I'd focus on getting the rotation correct first.
     
  3. Yoreki

    Yoreki

    Joined:
    Apr 10, 2019
    Posts:
    2,590
    This is a bit off topic and maybe subjective as well, but i find your code pretty horrible to read. Using variable names like "a", "r", "g" and so on makes it hard to follow along. This will become a lot worse when the script becomes longer. While you currently may not have a problem understanding your code, this will change after some time passed.
    Maybe you are used to it from some other language, but i dont think it's a good idea either way. Variables should have proper identifiers that explain what they contain in a minimalistic yet easy to understand way. So unless it's some physical constants with well known one character labels, one character is hardly ever enough to explain what a variable contains.
     
  4. kancelarija12

    kancelarija12

    Joined:
    Sep 17, 2019
    Posts:
    15
    I use it that way because it is faster and I'm not making a game that I will focus onthe most.
     
  5. Yoreki

    Yoreki

    Joined:
    Apr 10, 2019
    Posts:
    2,590
    It may be faster for you (short term only), but definitely not faster for others. As long as you need others to help with your problems, taking a second or two to think about a decent name might be worth considering.
    Of course this was just a suggestion and you are free to ignore it. Nobody is forcing you to do anything. However, nobody is forcing us to help people either. And if you were to post a longer script than that, using a similar naming sheme, then you shouldnt be confused as to why nobody bothers helping.