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 32
    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 <pre><code>using System.Collections; using System.Collections.Generic; using UnityEngine;

    public class KnifeController : WeaponController {

    <code>protected override void Start()
    {
        base.Start();
    }
    protected override void Attack()
    {
        base.Attack();
        GameObject spawnedKnife = Instantiate(weaponData.Prefab);
        spawnedKnife.transform.position = transform.position;
        spawnedKnife.GetComponent&lt;KnifeBehaviour&gt;().DirectionChecker(pm.lastMovedVector);
    }</code>

    }</code></pre>

    This is the KnifeBehaviour <pre><code>using System.Collections; using System.Collections.Generic; using UnityEngine;

    public class KnifeBehaviour : ProjectileWeaponBehaviour {

    <code>protected override void Start()
    {
        base.Start();
    
    }
    
    
    void Update()
    {
        transform.position += direction * weaponData.Speed * Time.deltaTime;
    }</code>

    }</code></pre>

    This is the ProjectileWeaponBehaviour <pre><code>using System.Collections; using System.Collections.Generic; using UnityEngine;

    public class ProjectileWeaponBehaviour : MonoBehaviour { public WeaponScriptableObject weaponData;

    <code>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 &lt; 0 &amp;&amp; diry == 0)
        {
            scale.x = scale.x * -1;
            scale.y = scale.y * -1;
        }
        else if (dirx == 0 &amp;&amp; diry &lt; 0)
        {
            scale.y = scale.y * -1;
        }
        else if (dirx == 0 &amp;&amp; diry &gt; 0)
        {
            scale.x = scale.x * -1;
        }
        else if (dir.x &gt; 0 &amp;&amp; dir.y &gt; 0)
        {
            rotation.z = 0f;
        }
        else if (dir.x &gt; 0 &amp;&amp; dir.y &lt; 0)
        {
            rotation.z = -90f;
        }
        else if (dir.x &lt; 0 &amp;&amp; dir.y &gt; 0)
        {
            scale.x = scale.x * -1f;
            scale.y = scale.y * -1f;
            rotation.z = -90f;
        }
        else if (dir.x &lt; 0 &amp;&amp; dir.y &lt; 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(&quot;Enemy&quot;))
        {
            EnemyStats enemy = col.GetComponent&lt;EnemyStats&gt;();
            enemy.TakeDamage(currentDamage);
        }
    }</code>

    }</code></pre>

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

    #13368
    Terence
    Level 32
    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 32
    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
    ::

    <a href=”https://imgur.com/a/p95T4lF“></a>

    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

    <blockquote class=”imgur-embed-pub” lang=”en” data-id=”a/p95T4lF”><a href=”https://imgur.com/a/p95T4lF” target=”_blank”>View post on imgur.com</a></blockquote><script async src=”//s.imgur.com/min/embed.js” charset=”utf-8″></script>

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

    Oliver, your Knife’s Rigidbody has its <strong>Simulated</strong> 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 32
    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: