Melee Raycast
Hello everyone. sorry if i format this wrong or something I am trying to make a first person fighting game and I am having problems with hitting and attacking movements. I have tried using the colliders and a target script which lowers a text number the HP on detection. The problem I was having was it caused mutiple detection's and lower'd the HP too much in one punch. I am trying to now use raycast as a means of detection. This is what I am trying to use for the ray, and getting a null reference error on line 18 for some reason. If someone could help with a script that would activate the ray on mouse click to the cursors location, for targeting purposes like hitting head or legs. Also if there is a way that the ray could be followed up visually so it looks like a punching motion.
I am still new to all this and I am trying to teach myself as I go, so if you could also try and explain a little as to why there is a way that works that would be very helpful so hopefully I can fix problems like this on my own in the future.
Thanks
tldr: trying to get a ray that detects hits on mouse click. like a punching movement.
void Start()
{
maincam = GetComponentInParent<Camera>();
}
void Update()
{
// Create a vector at the center of our camera's viewport
Vector3 lineOrigin = maincam.ViewportToWorldPoint(new Vector3(0.5f, 0.5f, 0.0f));
// Draw a line in the Scene View from the point lineOrigin in the direction of fpsCam.transform.forward , using the color green
Debug.DrawRay(lineOrigin, maincam.transform.forward , Color.green);
}
}
Your answer
![](https://koobas.hobune.stream/wayback/20220612110346im_/https://answers.unity.com/themes/thub/images/avi.jpg)