Search Unity

Resolved Why are bullets missing?

Discussion in 'Editor & General Support' started by Cosmology27, Jan 26, 2023.

  1. Cosmology27

    Cosmology27

    Joined:
    Jul 11, 2019
    Posts:
    61
    Here's a picture of what's happening during runtime. https://imgur.com/a/2dHNhwG
    Here's my relevant section of code. This script is going on the bullet itself. Basically, when the bullet is instantiated, this script tells it where to fly.
    Code (CSharp):
    1.     void Start()
    2.     {
    3.         BulletDir = DestinationPos - transform.position;
    4.         Debug.DrawRay(transform.position, BulletDir, Color.yellow, 99999999, false);
    5.     }
    6.  
    7.     void Update()
    8.     {
    9.         transform.Translate(BulletDir * BulletSpeed * Time.deltaTime);
    10.     }
    I'm sure this is just a stupid mistake on my part, but I believe I'm doing this correctly. The REALLY strange thing to me is that the debug ray is going EXACTLY where it should be going, on top of the big rectangular box. But the bullets, using the exact same destination, and the exact same method of calculating the direction vector, are going off to the right. Like if my math was wrong, you'd think the bullets would be going wildly in the wrong direction, but they're only slightly off. I'm just confused. Any ideas?
     
    Last edited: Jan 26, 2023
  2. chemicalcrux

    chemicalcrux

    Joined:
    Mar 16, 2017
    Posts:
    720
    By default, Translate works in local space, but the bullet direction is world-space.

    This will cause the bullet to move in the wrong direction if it has been rotated (and the more rotated it is, the more incorrect the movement will be). If it's been turned 90 degrees to the right, for example, the bullet's "forward" direction will be the world's "right" direction.

    I'd just add to
    transform.posiiton
    . Alternatively, you could pass
    Space.World
    as second argument.
     
    Cosmology27 likes this.
  3. Cosmology27

    Cosmology27

    Joined:
    Jul 11, 2019
    Posts:
    61
    Thanks so much! I knew it would be something simple I was ignorant of. Changed to world.space and it worked immediately. You're awesome thanks!