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

Viewing 8 posts - 1 through 8 (of 8 total)
  • Author
    Posts
  • #13390
    Esteban
    Former Patron

    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;
    }
    #13392
    Terence
    Keymaster

    Estaban, your code won’t work for 2 reasons:

    1. 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.
    2. 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.

    #13398
    Esteban
    Former Patron

    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!

    #13399
    Esteban
    Former Patron

    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?

    #13400
    Terence
    Keymaster

    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:

    1. 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.
    2. 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.

    #13401
    Terence
    Keymaster

    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!

    #13408
    Esteban
    Former Patron

    Thank you so much for the advise! ill go ahead and test both methods out appreciate your assistance

    #13414
    Terence
    Keymaster

    No problem Esteban. Thanks for your support.

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

Go to Login Page →


Advertisement below: