Forum begins after the advertisement:

[Part 3] Different way to handle shooting?

Home Forums Video Game Tutorial Series Creating a Rogue-like Shoot-em Up in Unity [Part 3] Different way to handle shooting?

Viewing 3 posts - 1 through 3 (of 3 total)
  • Author
  • #11832

    Reposting a YouTube comment from vitriolicAmaranth on Part 3 of the series, as he brings up a few interesting topics for discussion:

    Maybe you already changed this in a later part or maybe I’m gonna kick myself later because there’s a reason you did it differently (not really- I implemented your style of code, then commented it out and did it my way, so it’s all still there if I need it later), but wouldn’t it be better to adjust the direction of the projectile (and potentially the player sprite facing) using Mathf.Atan2(x, y) * Mathf.Rad2Deg? That way it supports analog aiming on controllers (like Vampire Survivors does) and uses way fewer lines of code, and you could even more easily adapt the code for a top-down 3D Survivors clone with a full range of player model rotation or add more player facings later on (sprites for up, down, upleft, etc). One issue with this method (but an easy one to fix, in one of two ways) is sprite rotation. This overrides any rotation you make in the prefab, so you either need to rotate the sprite in the image file itself (for some reason the Unity sprite editor does not have this capability, at least as far as I can tell) or add an offset to the rotation.

    //eg my ProjectileWeaponBehaviour.cs code has
    rotationDegrees = Mathf.Atan2(dir.x, dir.y) * Mathf.Rad2Deg;
    rotation = new Vector3(0, 0, -rotationDegrees + 90);
    //because the rotation you get this way is reversed (you need to use -float instead of just float with its output) and my sprite faces right, meaning I need to rotate it left (positive) 
    //90 degrees on the z axis to make 0 rotation match the "up" direction; yours would be -rotationDegrees + 45.
    //This still saves a lot of work and turns about 18 lines of code into 2 lines of code in the projectile weapon script.

    My bad, just saw that someone else made a similar comment 4 months ago.

    I also feel like there should be a more efficient way to handle chunk generation, but having no idea where to even start with procedural generation is a lot of the reason I started watching this series in the first place, so I don’t have any bright ideas there yet (plus I know that in a later part you have returned to and improved that, at least with regards to being able to jitter ungenerated corners onto the screen).

    Only other gripe I have so far (and I am more confident that this one isn’t going to cause any problems down the road, but more strongly suspect later in the tutorial it’s something that would get addressed) is that you can resolve a lot of the issues people have with their player or props or projectiles being hidden by the terrain by using sorting layers. Add layers like “player”, “enemies”, “projectiles”, “terrain” and so on and put each prefab or object on the appropriate layer, then just drag them into your preferred render order in the layers window so that for example the player renders on top of everything or almost everything and the terrain renders under everything (I left mine on “default” but this is incredibly easy to change later on if later on I want to make maps where there is something visible underneath the terrain, which I feel like I probably will for background parallax on maps meant to evoke a sense of height).


    Here is what KacperOgrodniczuk had to say in a different comment, which was a response to this question:

    At 19:00, If you’re using rigidbodies.velocity and not transform to move your projectiles you can use the following code to avoid having a bunch of if statements.

    targetDir = dir
    float angle = Mathf.Atan2(targetDir.y, targetDir.x) * Mathf.Rad2Deg;
    angle += spriteAngleOffset;
    transform.rotation = Quaternion.Euler(0, 0, angle);

    and the only thing you need to figure out is the sprite angle offset.

    For example:
    my sprite when rotated to 0, 0, 0 faces top right instead of right, meaning my spriteAngleOffset value is equal to -45.

    This also means you don’t have to mess with the objects scale, as you are exclusively dealing with the object rotation.

    Hope that helps, it’s a little bit more readable and I find it easier to wrap my head around this code, I think it might break if you try to use this piece of good and move the projectile using the transform component like in the tutorial but I’m not sure. Great tutorial btw!!!


    Note that in Amaranth’s post, he uses Mathf.Atan2( dir.x, dir.y ) * Mathf.Rad2Deg to get the angle of movement (represented by dir). It should be Mathf.Atan2( dir.y, dir.x ), because Atan2() is the tangent inverse operation — basically, it takes 2 values: 1) opposite, and 2) adjacent; and it calculates the angle of a right-angled triangle in a given side.

    You might be wondering: why are there triangles? We are talking about 2D vectors here! Well, 2D vectors are right-angled triangles in a sense!

    A 2D vector as a right-angled triangle

    The 2D vector’s angle we are interested finding is the angle on the left side, i.e. the angle labelled theta (θ) in the image below (ignore the other labels):

    The facing angle of a 2D vector

    These images are from 2 articles I wrote in the early days of this blog. You can check these articles out if you are interested to learn more about vector math!

Viewing 3 posts - 1 through 3 (of 3 total)
  • You must be logged in to reply to this topic.

Go to Login Page →

Advertisement below: