• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Symbie · Aug 26, 2020 at 01:19 PM · raycastmelee attackbots

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);
         }
     }
 }
 
Comment
Add comment · Show 3
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Llama_w_2Ls · Aug 26, 2020 at 02:14 PM 0
Share

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.

avatar image Symbie Llama_w_2Ls · Aug 26, 2020 at 11:16 PM 0
Share

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

avatar image Llama_w_2Ls Symbie · Aug 27, 2020 at 08:40 AM 0
Share

Well I dont think you can change the width of the ray, ive never seen anyone do that ever before. Sorry

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

182 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How could I combine melee weapons and rays? 1 Answer

2D melee Combat with SphereCastAll Problem 1 Answer

raycast help need 2 Answers

Push object in direction of camera 0 Answers

Raycast not colliding with selected layer 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges