- Home /
Changing a Raycast for different melee weapons
I am making a first person game and im using some melee weapons to attack the bots. at the moment i am using a raycast to detect if the bot has been hit, but all i can change is how far the weapon reaches, such as how the knife has shorter reach then the sword. but i would also like to change the width of the cast, so something like the sword can side-swipe and hit horizontally, or something like a hammer that has a bigger radius. i dont know how to do this though. i am also changing the reach and damage in a different script by using public static bools but it makes the code look bad and clunky. suggestions on how to fix that would also be appreciated. anyway, this is the code for the melee:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UIElements;
public class PlayerAttack : MonoBehaviour
{
Camera cam;
public static float range;
public static float damage;
public LayerMask layerMask;
private float timeBtwShots;
public static float startTimeBtwShots;
float waitTime;
public Animator knifeAnim;
public Animator swordAnim;
// Start is called before the first frame update
void Start()
{
cam = GetComponentInChildren<Camera>();
}
// Update is called once per frame
void Update()
{
waitTime = startTimeBtwShots / 2 ;
if(timeBtwShots <= 0)
{
if (Input.GetMouseButtonDown(0))
{
StartCoroutine(Attack());
timeBtwShots = startTimeBtwShots;
}
}
else
{
timeBtwShots -= Time.deltaTime;
}
}
IEnumerator Attack()
{
RaycastHit hit;
if (PlayerWeapon.usingKnife)
{
knifeAnim.SetBool("isSwinging", true);
if (Physics.Raycast(cam.transform.position, cam.transform.forward, out hit, range, layerMask))
{
if(hit.transform.tag == "Enemy")
{
Debug.DrawLine(cam.transform.position, hit.transform.position, Color.red, 3f);
hit.transform.gameObject.GetComponent<KnockBack>().applyKnockBack = true;
hit.transform.gameObject.GetComponent<BotHealth>().TakeDamage(damage);
}
else if(hit.transform.tag == "Squirrel")
{
hit.transform.gameObject.GetComponent<SquirrelHealth>().TakeDamage(damage);
}
}
SoundManager.knifeSwing = true;
yield return new WaitForSeconds(startTimeBtwShots);
knifeAnim.SetBool("isSwinging", false);
}
else if(PlayerWeapon.usingSword)
{
swordAnim.SetBool("isSwinging", true);
SoundManager.swordSwing = true;
yield return new WaitForSeconds(waitTime);
if (Physics.Raycast(cam.transform.position, cam.transform.forward, out hit, range, layerMask))
{
if(hit.transform.tag == "Enemy")
{
Debug.DrawLine(cam.transform.position, hit.transform.position, Color.red, 3f);
hit.transform.gameObject.GetComponent<KnockBack>().applyKnockBack = true;
hit.transform.gameObject.GetComponent<BotHealth>().TakeDamage(damage);
}
else if(hit.transform.tag == "Squirrel")
{
hit.transform.gameObject.GetComponent<SquirrelHealth>().TakeDamage(damage);
}
}
yield return new WaitForSeconds(waitTime);
swordAnim.SetBool("isSwinging", false);
}
}
}
I dont think you want a raycast for melee weapons, as its quite simple to just attach a collider to the weapon, animate the slash, and use the OnCollisionEnter(Collision collider) function to do the damage.
I already tried using a collider but the collision wasnt as smooth as i would like it to be, and it also makes the animation look bad because i'd have to either stretch the weapon, or move it forward for it to reach where i want it to reach, which is why i think a raycast fits better here. its just the width thats the problem
Well I dont think you can change the width of the ray, ive never seen anyone do that ever before. Sorry
Your answer
Follow this Question
Related Questions
How could I combine melee weapons and rays? 1 Answer
2D melee Combat with SphereCastAll Problem 1 Answer
Rotate an object perpendicular to an objects normals? 1 Answer
I am trying destroy a game object using ray cast! Not working? 0 Answers
NullReference when checking tag via Raycast. How to solve this? 1 Answer