Search Unity

Meele Script

Discussion in 'Scripting' started by Karwler, Aug 28, 2014.

  1. Karwler

    Karwler

    Joined:
    Aug 25, 2014
    Posts:
    18
    I'm looking for a good way to write a meele script. For now I have something like that
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class MyHand : MonoBehaviour {
    5.  
    6.     float Distance;
    7.     RaycastHit hit;
    8.    
    9.     void Update () {
    10.         if (Input.GetButton("Fire1")) {
    11.             audio.Play();
    12.             Vector3 forward = transform.TransformDirection (Vector3.forward);
    13.             Distance = hit.distance;
    14.             if (Distance <= 2) {
    15.                 hit.transform.SendMessage("ApplyDamage", 50, SendMessageOptions.DontRequireReceiver);
    16.             }
    17.         }
    18.     }
    19.  
    20. }
    21.  
    but it's pointing only at one point which is kinda inconvenient. Especially if the player swings his fist all across the whole screen^^
     
  2. Andrew Chami

    Andrew Chami

    Joined:
    Oct 27, 2012
    Posts:
    22
    I suggest you go watch a tutorial on raycasts. You can also watch brackeys survival game tutorial, it should help quiet abit.
     
  3. Karwler

    Karwler

    Joined:
    Aug 25, 2014
    Posts:
    18
    That's exactly what I'm trying not to use and hat's why I asked if there is another way to do it.
     
  4. Ereous

    Ereous

    Joined:
    Aug 29, 2012
    Posts:
    163
    Look at Physics.Overlapsphere and Vector3.Angle

    Using these you can get all things hit within an arc. Basically doing this: