Projectile Firing Query

Good day,

I am really close to having a functional projection for my “bullets” in my top down game. Issue I am having is if I stand still and concentrate the fire on a point without my mouse, the projectiles stop projecting straight out to the wall and they fall off and start colliding with the ground plane

(images located here make this easier to explain) Imgur: The magic of the Internet

If I move the mouse cursor around the bullets then go back to firing as expected (out until they collide with the walls)

I’m running 4 scripts in total in this scene ( all attached)

  • Player Direction: uses a ray to make my character aim in the direction of my mouse
  • TopDownCharacterMovement: is my character controller for movement + a dash ability that dashes in the direction I am moving (not facing)
  • ProjectileMovement: Just a collision script.
  • ProjectileShoot: Ray cast for my bullet which is a prefab particle system

Any ideas what could be going on? I believe it to be line 35 in ProjectileShoot as I was struggling with positioning my raycast relative to my mouse direction and that seemed to work at first. I did try some other variations of getmousecursor but then my bullet was going to my mouse cursor’s location and not say… straight out to infinity (or to a collision with the wall).

To be clear I want zero Y direction on the bullet. only X and Z.

7511711–926162–PlayerDirection.cs (638 Bytes)
7511711–926165–ProjectileMovement.cs (369 Bytes)
7511711–926168–ProjectileShoot.cs (1.33 KB)
7511711–926171–TopDownCharacterMovement.cs (1.12 KB)

You must find a way to get the information you need in order to reason about what the problem is.

What is often happening in these cases is one of the following:

  • the code you think is executing is not actually executing at all
  • the code is executing far EARLIER or LATER than you think
  • the code is executing far LESS OFTEN than you think
  • the code is executing far MORE OFTEN than you think
  • the code is executing on another GameObject than you think it is

To help gain more insight into your problem, I recommend liberally sprinkling Debug.Log() statements through your code to display information in realtime.

Doing this should help you answer these types of questions:

  • is this code even running? which parts are running? how often does it run? what order does it run in?
  • what are the values of the variables involved? Are they initialized? Are the values reasonable?
  • are you meeting ALL the requirements to receive callbacks such as triggers / colliders (review the documentation)

Knowing this information will help you reason about the behavior you are seeing.

You can also put in Debug.Break() to pause the Editor when certain interesting pieces of code run, and then study the scene

You could also just display various important quantities in UI Text elements to watch them change as you play the game.

If you are running a mobile device you can also view the console output. Google for how on your particular mobile target.

Here’s an example of putting in a laser-focused Debug.Log() and how that can save you a TON of time wallowing around speculating what might be going wrong:

https://discussions.unity.com/t/839300/3

1 Like

After running a Debug as you suggested, all of the c

Thank you for the reply.

I think the issue is my lack of understand of the C# language to being with (I didn’t even know what Debug Log() was and had to watch some youtube videos on it)… still not certain how to use it to be honest.

I’m still have the issue with this script and I am tearing my hair out trying to make it work. Maybe a step back and learning more of the “basics” is in order

I’ve managed to fix all the issues except one. It appears my character, or something associated with my character is colliding with my bullets and causing them to destroy / bounce all over the screen. I am using the code below to destroy my bullets. I’ve also gone ahead and tagged what I believe to be everything I want excluded with either Player, or Bullet.

To test, I turn the speed of my character to be greater than the projectile speed and as suspected as I walk into my bullets they disappear and my character “jitters” on the screen as if it interact with them.

“player” consists of the following all with tags (Player), and are all children of an empty object with a Character Controller:

  1. Cylinder Mesh + Mesh Renderer
  2. Cube Mesh + Mesh Renderer (just to show the direction my cylinder is facing)
  3. a bullet spawn point which id a 3d empty object located in the cube listed above.

The “bullet” consists of:

  1. Sphere mesh + renderer
  2. Sphere Collider (is Trigger UNCHECKED)
  3. Rigidbody

Environment is simply a plane, +4 walls with tags (Environment)

I have a Main Camera (w/ a CM virtual cam), + 1 directional light

Any suggestions on a path forward to figure out what it’s colliding with?

EDIT: Fixed this by putting all of the “player” items on a layer called Player, and the Bullet on a layer called “Bullet” and disabling the physics interactions under project manager.

I am open to better solutions

 void OnCollisionEnter(Collision co)
    {
        if(co.gameObject.tag != "Bullet" && co.gameObject.tag != "Player" && !collided)
        {
            collided = true;
            Destroy(gameObject);
        }
    }

I think that’s kinda the best solution! You’re telling the physics engine not to even consider certain classes of collisions, and that’s exactly what that matrix is for.

1 Like