Forum begins after the advertisement:


[Part 4] Knife wont detect when colliding with bat

Home Forums Video Game Tutorial Series Creating a Rogue-like Shoot-em Up in Unity [Part 4] Knife wont detect when colliding with bat

Viewing 11 posts - 1 through 11 (of 11 total)
  • Author
    Posts
  • #13358
    Oliver Murchie
    Level 3
    Participant
    Helpful?
    Up
    0
    ::

    I feel as if I have followed the code on the video and yet I cant seem to get it to work, I tried using a Debug.Log to see if it was colliding and it didn’t show up

    #13365
    Terence
    Level 30
    Keymaster
    Helpful?
    Up
    0
    ::

    Hi Oliver, check if you have a Rigidbody2D on either the bat or the knife prefab. Collisions need a Rigidbody on at least one of the objects to register.

    If that doesn’t work, you’ll need to paste your code here for the Knife.

    #13367
    Oliver Murchie
    Level 3
    Participant
    Helpful?
    Up
    0
    ::

    This is the KnifeController

    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;
            spawnedKnife.GetComponent<KnifeBehaviour>().DirectionChecker(pm.lastMovedVector);
        }
    }

    This is the KnifeBehaviour

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    
    public class KnifeBehaviour : ProjectileWeaponBehaviour
    {
        
    
        protected override void Start()
        {
            base.Start();
            
        }
    
        
        void Update()
        {
            transform.position += direction * weaponData.Speed * Time.deltaTime;
        }
    }

    This is the ProjectileWeaponBehaviour

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    
    public class ProjectileWeaponBehaviour : MonoBehaviour
    {
        public WeaponScriptableObject weaponData;
    
        protected Vector3 direction;
        public float destoryAfterSeconds;
    
        //Current Weapon Stats
        protected float currentDamage;
        protected float currentSpeed;
        protected float currentCooldownDuration;
        protected int currentPierce;
    
        void Awake()
        {
            currentDamage = weaponData.Damage;
            currentSpeed = weaponData.Speed;
            currentCooldownDuration = weaponData.CooldownDuration;
            currentPierce = weaponData.Pierce;
            
        }
    
        protected virtual void Start()
        {
            Destroy(gameObject, destoryAfterSeconds);
        }
    
        public void DirectionChecker(Vector3 dir)
        {
            direction = dir;
    
            float dirx = direction.x;
            float diry = direction.y;
    
            Vector3 scale = transform.localScale;
            Vector3 rotation = transform.rotation.eulerAngles;
    
            if (dirx < 0 && diry == 0)
            {
                scale.x = scale.x * -1;
                scale.y = scale.y * -1;
            }
            else if (dirx == 0 && diry < 0)
            {
                scale.y = scale.y * -1;
            }
            else if (dirx == 0 && diry > 0)
            {
                scale.x = scale.x * -1;
            }
            else if (dir.x > 0 && dir.y > 0)
            {
                rotation.z = 0f;
            }
            else if (dir.x > 0 && dir.y < 0)
            {
                rotation.z = -90f;
            }
            else if (dir.x < 0 && dir.y > 0)
            {
                scale.x = scale.x * -1f;
                scale.y = scale.y * -1f;
                rotation.z = -90f;
            }
            else if (dir.x < 0 && dir.y < 0)
            {
                scale.x = scale.x * -1f;
                scale.y = scale.y * -1f;
                rotation.z = 0f;
            }
    
            transform.localScale = scale;
            transform.rotation = Quaternion.Euler(rotation);
        }
    
        protected virtual void OnTriggerEnter2D(Collider2D col)
        {
            if (col.CompareTag("Enemy"))
            {
                EnemyStats enemy = col.GetComponent<EnemyStats>();
                enemy.TakeDamage(currentDamage);
            }
        }
    }

    I checked and the knife has a rigidbody attached so I am unaware of where I have gone wrong

    #13368
    Terence
    Level 30
    Keymaster
    Helpful?
    Up
    0
    ::

    Did you forget to tag the Bat as an “Enemy”?

    #13369
    Oliver Murchie
    Level 3
    Participant
    Helpful?
    Up
    0
    ::

    No I made sure to double check that as well

    #13370
    Terence
    Level 30
    Keymaster
    Helpful?
    Up
    0
    ::

    Try pausing the game with a knife / bat out and check if the spawned knife has a rigidbody / bat has the correct tag.

    Also check that you’re using a Rigidbody2D, not a regular Rigidbody.

    #13371
    Oliver Murchie
    Level 3
    Participant
    Helpful?
    Up
    0
    ::

    I have two pictures showing that when paused the bat is tagged as enemy and the knife has a rigidbody attached

    #13372
    Oliver Murchie
    Level 3
    Participant
    Helpful?
    Up
    0
    ::

    Photos are here

    View post on imgur.com

    #13375
    Terence
    Level 30
    Keymaster
    Helpful?
    Up
    0
    ::

    Oliver, your Knife’s Rigidbody has its Simulated property unchecked. Can you check it and make sure its saved to the prefab, then try again?

    #13378
    Oliver Murchie
    Level 3
    Participant
    Helpful?
    Up
    0
    ::

    It worked, thank you so much. I feel quite silly that I didnt think about that

    #13425
    Terence
    Level 30
    Keymaster
    Helpful?
    Up
    0
    ::

    Glad I managed to help you fix your issue Oliver.

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

Go to Login Page →


Advertisement below: