- Home /
Raycast/Melee Attack Help Please!
Okay so I'm making a melee weapon script that uses a raycast to get the distance from you to the enemy and when the enemy comes within distance and the player clicks, it does damage to the enemy. i'm just having trouble getting this to work.. any help would be appreciated!
These are the errors:
Error CS1502: The best overloaded method match for 'UnityEngine.Physics.Raycast(UnityEngine.Vector3, UnityEngine.Vector3, out UnityEngine.RaycastHit)' has some invalid arguments (CS1502) (Assembly-CSharp)
Error CS1620: Argument '3' must be passed with the 'out' keyword (CS1620) (Assembly-CSharp)
using UnityEngine;
using System.Collections;
public class MeleeSystem : MonoBehaviour {
int Damage = 50;
float Distance;
float MaxDistance = 2;
Transform TheSystem;
void Update (){
if (Input.GetButtonDown("Fire1"))
{
TheSystem.animation.Play("melee_attack");
RaycastHit hit;
if(Physics.Raycast (transform.position, transform.TransformDirection(Vector3.forward), hit))
{
Distance = hit.distance;
if (Distance < MaxDistance)
hit.transform.SendMessage("ApplyDamage", Damage, SendMessageOptions.DontRequireReceiver);
}
}
}
}
Answer by whydoidoit · Mar 11, 2014 at 11:40 AM
You want your raycast to line to look like this:
if(Physics.Raycast(transform.position, transform.forward, out hit))
{
...
You need an "out" in front of hit and you can just use transform.forward
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Raycast causes all enemies to attack 1 Answer
Player Attack Script. I Need Help! 0 Answers
The name `target' does not exist in the current context 1 Answer