Forum begins after the advertisement:
[Part 8] Coding a homing / auto-aiming Knife
Home › Forums › Video Game Tutorial Series › Creating a Rogue-like Shoot-em Up in Unity › [Part 8] Coding a homing / auto-aiming Knife
- This topic has 7 replies, 2 voices, and was last updated 9 months ago by Terence.
-
AuthorPosts
-
February 23, 2024 at 5:47 am #13390::
If i wanted my knife to auto aim to the enemy can i make a script thats like enemy movement and just swap out the player for enemy?
void Start() { direction = Enemy.instance.transform.position - transform.position; direction.Normalize(); }
void Update() { transform.position += direction * speed * Time.deltaTime; }
February 23, 2024 at 10:48 am #13392::Estaban, your code won’t work for 2 reasons:
- The direction vector needs to be calculated in the
Update()
function, as it needs to be refreshed to be able to home in to the target. You can see Part 17’s article for more info on the math for this. - You can’t use
Enemy.Instance
as well, because that will always point to the same enemy. You will need to pick a random enemy from the scene every time the knife fires, and there are a couple of ways to do that.
Which part of the series are you on? In Part 15, we revamped the system and made it easier to make homing projectiles.
February 23, 2024 at 2:56 pm #13398::Ah okay i wasn’t at that part yet and was just trying to set up auto aim i am on part 8 i will look at that thank you!
February 23, 2024 at 3:25 pm #13399::Hey Terence im going through the video for part 15 to see those steps if i did not want to have multiple characters im assuming that i would just have to remove the selectedweaponsubtype code from the scripts? i went through the code and i removed passive items script and code lines as well as characterselector but my weapon wont spawn would not having that script into my code be causing that issue since you were coding this with the intent to initially select a character?
February 23, 2024 at 10:56 pm #13400::Hi Esteban, the Part 15 contents use a weapon system that is radically different from the earlier parts. It will be difficult for you to patch in some parts of it onto your codebase.
You have 2 options:
- Download the project files from Part 15 and you can work on your own game from there. It may be a bit more challenging to grasp, but if you think you are up to the task, you will learn a lot faster because you are learning from the top down.
- Continue following the series from where you are at. If you choose to do this, you will learn a lot slower, but you will build better fundamentals in terms of coding and your understanding of the project files. If you choose to do this, post me the code for your Knife projectile here and I will modify it for you to auto-aim and explain to you how it works.
Personally, I picked up games programming doing something similar to 1 (I didn’t have the patience to learn it from the ground up — I just began modding games and finding my way). I learnt really quickly this way but it gave me a lot of frustration as well.
February 23, 2024 at 11:17 pm #13401::Here’s how I would modify
KnifeBehaviour
from Part 8 to make it homing:using System.Collections; using System.Collections.Generic; using UnityEngine; public class KnifeBehaviour : ProjectileWeaponBehaviour { public EnemyStats target; // Current enemy we are targetting. protected override void Start() { base.Start(); } void Update() { // If this knife is flying towards a target, we will have to constantly point it towards it. if(target) { direction = (target.position - transform.position).normalized; } transform.position += direction * currentSpeed * Time.deltaTime; //Set the movement of the knife } }
Then, for
KnifeController
, you will need to assign a target to it:using System.Collections; using System.Collections.Generic; using UnityEngine; public class KnifeController : WeaponController { protected override void Start() { base.Start(); } protected override void Attack() { base.Attack(); GameObject spawnedKnife = Instantiate(weaponData.Prefab); spawnedKnife.transform.position = transform.position; //Assign the position to be the same as this object which is parented to the player // Try to find a target for the knife. EnemyStats target = null; // Get all enemies on the scene. EnemyStats[] allEnemies = FindObjectsOfType<EnemyStats>(); // Pick a random enemy. if(allEnemies.Length > 0) target = allEnemies[ Random.Range(0, allEnemies.Length) ]; // If a target is found, set the target for the spawned knife so it will home towards it. if(target) { spawnedKnife.target = target; } spawnedKnife.GetComponent<KnifeBehaviour>().DirectionChecker(pm.lastMovedVector); //Reference and set the direction } }
I’ve included comments to explain what the code does. I hope it helps! Feel free to clarify if any part of the code confuses you. I will be glad to respond.
For the vector math regarding the knife homing, this post (that I wrote a long time ago) will be helpful as well: https://blog.terresquall.com/2020/01/vector-math-for-polar-movement-in-2d-games-unity/
Note: I haven’t tested the code. I put it together for you in about 30 minutes or so, so there may be some minor errors that you have to weed out and fix. Fingers crossed this works!
February 24, 2024 at 12:47 am #13408::Thank you so much for the advise! ill go ahead and test both methods out appreciate your assistance
February 24, 2024 at 12:52 pm #13414 - The direction vector needs to be calculated in the
-
AuthorPosts
- You must be logged in to reply to this topic.
Advertisement below: