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
- This topic has 11 replies, 2 voices, and was last updated 10 months ago by Terence.
-
AuthorPosts
-
February 20, 2024 at 5:28 am #13358::
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
February 21, 2024 at 12:40 am #13365::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.
February 21, 2024 at 1:17 am #13367::This is the KnifeController
<code>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); } }</code>
This is the KnifeBehaviour
<code>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; } }</code>
This is the ProjectileWeaponBehaviour
<code>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); } } }</code>
I checked and the knife has a rigidbody attached so I am unaware of where I have gone wrong
February 21, 2024 at 1:23 am #13368February 21, 2024 at 1:24 am #13369February 21, 2024 at 1:28 am #13370::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.
February 21, 2024 at 1:33 am #13371::I have two pictures showing that when paused the bat is tagged as enemy and the knife has a rigidbody attached
February 21, 2024 at 1:33 am #13372February 21, 2024 at 11:05 pm #13375::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?
February 21, 2024 at 11:25 pm #13378February 28, 2024 at 11:27 pm #13425 -
AuthorPosts
- You must be logged in to reply to this topic.
Advertisement below: